1. 程式人生 > >springboot原始碼解析-管中窺豹系列之EnableXXX(十)

springboot原始碼解析-管中窺豹系列之EnableXXX(十)

# 一、前言 - Springboot原始碼解析是一件大工程,逐行逐句的去研究程式碼,會很枯燥,也不容易堅持下去。 - 我們不追求大而全,而是試著每次去研究一個小知識點,最終聚沙成塔,這就是我們的springboot原始碼管中窺豹系列。 ![ 簡介 ](https://zhangbin1989.gitee.io/blog/picture/zb0018_springsour/springboot_source_0.png) # 二、EnableXXX - 我們上一節講了自動裝配,用到了@SpringBootApplication裡面的@EnableAutoConfiguration - springboot還封裝了其它的EnableXXX註解 - 比如我們想開啟定時任務,要加上註解:@EnableScheduling - 比如我們想用非同步程式設計,要加上註解:@EnableAsync - 自動裝配用的是:@Import(AutoConfigurationImportSelector.class) - 是不是都是這個套路呢?我們研究一下 # 三、原始碼分析 我們先看看@EnableScheduling ``` @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(SchedulingConfiguration.class) @Documented public @interface EnableScheduling { } ``` 同樣的用到@Import,我們看看SchedulingConfiguration ``` @Configuration @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class SchedulingConfiguration { @Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME) @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() { return new ScheduledAnnotationBeanPostProcessor(); } } ``` - 定義了一個BeanPostProcessor :ScheduledAnnotationBeanPostProcessor - BeanPostProcessor,我們先簡單的說下,相當於一個aop元件,在bean載入的時候呼叫 ``` public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } @Nullable default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } } ``` 我們看看ScheduledAnnotationBeanPostProcessor怎麼實現的方法? ``` public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof AopInfrastructureBean) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class targetClass = AopProxyUtils.ultimateTargetClass(bean); if (!this.nonAnnotatedClasses.contains(targetClass)) {