Spring动态代理默认使用CGlib,是因为它可以代理那些没有实现任何接口的类,而JDK动态代理仅能代理实现了接口的类。
CGlib相对于JDK动态代理来说,在代理类的创建和执行的速度上更快,因此在某些情况下,使用CGlib代理可以提高系统性能。
如果Spring是JDK代理,那么就会导致某些注解失效。
因
——————————————————End————————————————
As we all know, the underlying AOP is dynamic proxies, and there are two ways to implement dynamic proxies in Java:
The biggest difference between these two is that JDK-based dynamic proxies require the object being proxied to implement an interface, while Cglib-based dynamic proxies do not require the object being proxied to implement an interface.
So, how is AOP implemented in Spring? Is it a dynamic proxy based on JDK or a dynamic proxy based on Cglib?
Let’s start with the conclusion that dynamic proxies in Spring, which one to use, are divided into cases.
If the proxy object implements the interface, then use the JDK dynamic proxy, otherwise it is the Cglib dynamic proxy.
If the proxy object does not implement an interface, then it is a direct Cglib dynamic proxy.
Spring Boot and Spring are the same, so is it the same strategy for dynamic proxies? Sorry, it’s not really the same.
The handling of this issue in Spring Boot, with Spring Boot 2.0 as the node, is not the same.
Before Spring Boot 2.0, the code for automating the configuration of Aop looked like this (Spring Boot 1.5.22.RELEASE)
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
matchIfMissing = true)
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
matchIfMissing = false)
public static class CglibAutoProxyConfiguration {
}
}
As you can see, this automation configuration is mainly discussing the value of the spring.aop.proxy-target-class property in the application.properties configuration file.
The @ConditionalOnProperty annotation is what does the trick. To illustrate a few of the properties in this annotation.
Based on the introduction as above, it is easy to see that.
Let’s look at the situation after Spring Boot 2.0 (inclusive) (Spring Boot 2.0.0.RELEASE).
@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration {
}
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration {
}
}
As you can see, most of the configuration is the same, with one area that is not quite the same, and that is the value of the matchIfMissing property.
As you can see, starting with Spring Boot 2.0, if the user does not configure anything, the Cglib proxy is used by default.
来自Springboot的一篇英语文章