一、前言

  • Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去。
  • 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot源码管中窥豹系列。

二、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)) {
Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
(MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
Set<Scheduled> scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
method, Scheduled.class, Schedules.class);
return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
});
if (annotatedMethods.isEmpty()) {
this.nonAnnotatedClasses.add(targetClass);
if (logger.isTraceEnabled()) {
logger.trace("No @Scheduled annotations found on bean class: " + targetClass);
}
}
else {
// Non-empty set of methods
annotatedMethods.forEach((method, scheduledMethods) ->
scheduledMethods.forEach(scheduled -> processScheduled(scheduled, method, bean)));
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
"': " + annotatedMethods);
}
}
}
return bean;
}
  • 根据@Scheduled注解,执行相应的定时任务,不细看了

  • 我们在看看@EnableAsync


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync { Class<? extends Annotation> annotation() default Annotation.class; boolean proxyTargetClass() default false; AdviceMode mode() default AdviceMode.PROXY; int order() default Ordered.LOWEST_PRECEDENCE; }
  • 通过@Import加载AsyncConfigurationSelector
  • ImportSelector之前说过,通过接口返回的字符串数组,加载bean, 不细看了

好了,我们总结一下:

欢迎关注微信公众号:丰极,更多技术学习分享。

最新文章

  1. Cnblogs自定义皮肤css样式-星空观测者
  2. 配置oracle instance client
  3. Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
  4. 有了JSON.stringify(),处理json将变得更简单!!
  5. js 人工获取年月日
  6. HW4.12
  7. [置顶] .NET下枚举类型的Save和Load分析
  8. Mac中QT程序发布
  9. php学习之道:php中soap的使用实例以及生成WSDL文件,提供自己主动生成WSDL文件的类库——SoapDiscovery.class.php类
  10. Android进程间通信(IPC)机制Binder简介和学习计划
  11. C++学习笔记13-类继承
  12. x+y=xy
  13. UCS业务知识介绍
  14. [ZJOI 2010]count 数字计数
  15. Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Unable to compile class for JSP] with root cause java.lang.IllegalArgumentException: Page directive: inval
  16. PAT 乙级 1080 MOOC期终成绩 (25 分)
  17. PEP8规范
  18. map的循环删除操作
  19. 面试 -- Http协议相关(转载)
  20. unity3d中gameObject捕获鼠标点击

热门文章

  1. POJ2429 GCD &amp; LCM Inverse pollard_rho大整数分解
  2. python常用连接字符串
  3. 揭秘井井有条的流水线(ZooKeeper 原理篇)
  4. 男孩周末班-k8s-架构图
  5. 一些简单的SQL语句
  6. 2019牛客多校第一场I Points Division(DP)题解
  7. 解决.dll类等文件丢失或出错
  8. 微信小程序批量上传图片 All In One
  9. HTTP vs HTTP/2 vs HTTP/3 (QUIC)
  10. Python学习笔记_购物车案例