“Spring Boot 2 自动配置流程”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
| 第1行: | 第1行: | ||
https://www.bilibili.com/video/BV19K4y1L7MT?p=15 | https://www.bilibili.com/video/BV19K4y1L7MT?p=15 | ||
| − | 以 spring-boot-autoconfigure-2.3.4.RELEASE.jar 中的 org.springframework.boot.autoconfigure.aop.AopAutoConfiguration 为例,分析自动配置流程 | + | 以 spring-boot-autoconfigure-2.3.4.RELEASE.jar 中的 org.springframework.boot.autoconfigure.aop.AopAutoConfiguration 为例,分析自动配置流程<syntaxhighlight lang="java"> |
| + | …… | ||
| + | @Configuration(proxyBeanMethods = false) | ||
| + | @ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true) | ||
| + | public class AopAutoConfiguration { | ||
| + | |||
| + | @Configuration(proxyBeanMethods = false) | ||
| + | @ConditionalOnClass(Advice.class) | ||
| + | static class AspectJAutoProxyingConfiguration { | ||
| + | |||
| + | @Configuration(proxyBeanMethods = false) | ||
| + | @EnableAspectJAutoProxy(proxyTargetClass = false) | ||
| + | @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", | ||
| + | matchIfMissing = false) | ||
| + | static class JdkDynamicAutoProxyConfiguration { | ||
| + | |||
| + | } | ||
| + | |||
| + | @Configuration(proxyBeanMethods = false) | ||
| + | @EnableAspectJAutoProxy(proxyTargetClass = true) | ||
| + | @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", | ||
| + | matchIfMissing = true) | ||
| + | static class CglibAutoProxyConfiguration { | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | @Configuration(proxyBeanMethods = false) | ||
| + | @ConditionalOnMissingClass("org.aspectj.weaver.Advice") | ||
| + | @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", | ||
| + | matchIfMissing = true) | ||
| + | static class ClassProxyingConfiguration { | ||
| + | |||
| + | ClassProxyingConfiguration(BeanFactory beanFactory) { | ||
| + | if (beanFactory instanceof BeanDefinitionRegistry) { | ||
| + | BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; | ||
| + | AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry); | ||
| + | AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | } | ||
| + | </syntaxhighlight><code>@Configuration</code> 说明这是一个配置类; | ||
| + | |||
| + | <code>@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)</code> | ||
| + | |||
| + | 配置文件中是否存在 <code>spring.aop</code> 这个配置,如果存在了 <code>spring.aop.auto</code> 这个配置,并且它的值是“true”,那么这个类中的其他配置就生效,<code>matchIfMissing = true</code> 就算没有响应的配置,也认为满足条件,所以相当于这个配置是生效的 | ||
2023年3月5日 (日) 06:22的版本
https://www.bilibili.com/video/BV19K4y1L7MT?p=15
以 spring-boot-autoconfigure-2.3.4.RELEASE.jar 中的 org.springframework.boot.autoconfigure.aop.AopAutoConfiguration 为例,分析自动配置流程
……
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Advice.class)
static class AspectJAutoProxyingConfiguration {
@Configuration(proxyBeanMethods = false)
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
matchIfMissing = false)
static class JdkDynamicAutoProxyConfiguration {
}
@Configuration(proxyBeanMethods = false)
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
matchIfMissing = true)
static class CglibAutoProxyConfiguration {
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("org.aspectj.weaver.Advice")
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
matchIfMissing = true)
static class ClassProxyingConfiguration {
ClassProxyingConfiguration(BeanFactory beanFactory) {
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
}
}
}
@Configuration 说明这是一个配置类;
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
配置文件中是否存在 spring.aop 这个配置,如果存在了 spring.aop.auto 这个配置,并且它的值是“true”,那么这个类中的其他配置就生效,matchIfMissing = true 就算没有响应的配置,也认为满足条件,所以相当于这个配置是生效的