@Enable** 注解,通常用于开启某一类功能。相似于一种开关,只有加了这个注解,才能使用某些功能。html
spring boot 中常常遇到这样的场景,老大让你写一个定时任务脚本、开启一个spring缓存,或者让你提供spring 异步支持。你的作法确定是 @EnableScheduling+@Scheduled,@EnableCaching+@Cache,@EnableAsync+@Async 立马开始写逻辑了,但你是否真正了解其中的原理呢?以前有写过一个项目,是日志系统,其中要提供spring 注解支持,简化配置,当时就是参考以上源码的技巧实现的。java
先来看@EnableScheduling源码spring
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {
}
复制代码
能够看到这个注解是一个混合注解,和其余注解的惟一区别就是多了一个@Import注解api
经过查询spring api文档缓存
Indicates one or more @Configuration classes to import. Provides functionality equivalent to the element in Spring XML. Allows for importing @Configuration classes, ImportSelector and ImportBeanDefinitionRegistrar implementations, as well as regular component classes (as of 4.2; analogous to AnnotationConfigApplicationContext.register(java.lang.Class<?>...)). 表示要导入的一个或多个@Configuration类。 提供与Spring XML中的元素等效的功能。 容许导入@Configuration类,ImportSelector和ImportBeanDefinitionRegistrar实现,以及常规组件类(从4.2开始;相似于AnnotationConfigApplicationContext.register(java.lang.Class <?> ...))。app
能够看出,经过这个注解的做用是导入一些特定的配置类,这些特定类包括三种框架
先来看看导入@Configuration注解的例子,打开SchedulingConfiguration类异步
发现他是属于第一种,直接注册了一个ScheduledAnnotationBeanPostProcessor 的 Beanide
简单介绍一下ScheduledAnnotationBeanPostProcessor这个类干了什么事,他实现了BeanPostProcessor类。这个类能够在bean初始化后,容器接管前实现本身的逻辑。在bean 初始化以后,经过AnnotatedElementUtils.getMergedRepeatableAnnotations()方法去拿到当前bean有@Scheduled和@Schedules注解的方法。若是有的话,将其注册到内部ScheduledTaskRegistrar变量中,开启定时任务并执行。顺便说一下,BeanPostProcessor接口对全部bean适用,每一个要注册的bean都会走一遍postProcessAfterInitialization方法。post
能够看出,这种方法适用于初始化时便获取到所有想要的信息,如@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;
}
复制代码
能够看到他经过导入AsyncConfigurationSelector类来开启异步支持,打开AsyncConfigurationSelector类
public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {
private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
"org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";
@Override
public String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[] { ProxyAsyncConfiguration.class.getName() };
case ASPECTJ:
return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };
default:
return null;
}
}
}
复制代码
AdviceModeImportSelector是一个抽象类,他实现了ImportSelector类的selectImports方法,先来看一下selectImports的api 文档
Interface to be implemented by types that determine which @Configuration class(es) should be imported based on a given selection criteria, usually one or more annotation attributes. An ImportSelector may implement any of the following Aware interfaces, and their respective methods will be called prior to selectImports(org.springframework.core.type.AnnotationMetadata):
ImportSelectors are usually processed in the same way as regular @Import annotations, however, it is also possible to defer selection of imports until all @Configuration classes have been processed (see DeferredImportSelector for details). 经过一个给定选择标准的类型来肯定导入哪些@Configuration,他和@Import的处理方式相似,只不过这个导入Configuration能够延迟到全部Configuration都加载完
总结起来有一下几点:
查看@EnableAspectJAutoProxy
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
复制代码
这个注解导入的时AspectJAutoProxyRegistrar类,AspectJAutoProxyRegistrar实现了
ImportBeanDefinitionRegistrar接口,实现类
public void registerBeanDefinitions( AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
AnnotationAttributes enableAspectJAutoProxy =
AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
}
复制代码
咱们来看一下ImportBeanDefinitionRegistrar官方api文档
Interface to be implemented by types that register additional bean definitions when processing @Configuration classes. Useful when operating at the bean definition level (as opposed to @Bean method/instance level) is desired or necessary. Along with @Configuration and ImportSelector, classes of this type may be provided to the @Import annotation (or may also be returned from an ImportSelector).
这个接口的两个参数,AnnotationMetadata 表示当前类的注解,BeanDefinitionRegistry 注册bean。
能够看出和前两种方式比,这种方式更加精细,须要你本身去实现bean的注册逻辑。第二种方式只传入了一个AnnotationMetadata,返回类全限定名,框架自动帮你注册。而第三种方式,还传入了一个BeanDefinitionRegistry让你本身去注册。
其实三种方式都能很好的实现导入逻辑。他们的优缺点以下:
最后,咱们须要来写一个自实现的@EnableDisconfig功能。disconfig是一种配置中心,咱们通常的用法是写两个bean
@Bean(destroyMethod="destroy")
public DisconfMgrBean disconfMgrBean(){
.....
}
@Bean(initMethod="int", destroyMethod="destroy")
public DisconfMgrBeanSecond disconfMgrBeanSecond(){
......
}
复制代码
每次搭框架这么写确实挺费事的,即便你记在笔记上了,复制粘贴也还须要改scan路径。下面咱们用优雅的代码来实现一下。
首先定义一个注解类@EnableDisconf
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DisconfConfig.class})
public @interface EnableDisconf{
String scanPackages() default "";
}
复制代码
接着实现DisconfConfig类
@Configuration
public class DisconfConfig implements ApplicationContextAware {
private ApplicationContext applicationContext;
public DisconfConfig() {
}
@Bean(
destroyMethod = "destroy"
)
@ConditionalOnMissingBean
public DisconfMgrBean disconfMgrBean() {
DisconfMgrBean bean = new DisconfMgrBean();
Map<String, Object> bootBeans = this.applicationContext.getBeansWithAnnotation(EnableDisconf.class);
Set<String> scanPackagesList = new HashSet();
if (!CollectionUtils.isEmpty(bootBeans)) {
Iterator var4 = bootBeans.entrySet().iterator();
while(var4.hasNext()) {
Entry<String, Object> configBean = (Entry)var4.next();
Class<?> bootClass = configBean.getValue().getClass();
if (bootClass.isAnnotationPresent(EnableDisconf.class)) {
EnableDisconf enableDisconf = (EnableDisconf)bootClass.getAnnotation(EnableDisconf.class);
String scanPackages = enableDisconf.scanPackages();
if (StringUtils.isEmpty(scanPackages)) {
scanPackages = bootClass.getPackage().getName();
}
scanPackagesList.add(scanPackages);
}
}
}
if (CollectionUtils.isEmpty(scanPackagesList)) {
bean.setScanPackage(System.getProperty("scanPackages"));
} else {
bean.setScanPackage(StringUtils.join(scanPackagesList, ","));
}
return bean;
}
@Bean(
initMethod = "init",
destroyMethod = "destroy"
)
@ConditionalOnMissingBean
public DisconfMgrBeanSecond disconfMgrBeanSecond() {
return new DisconfMgrBeanSecond();
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
复制代码
这里有两点须要说明
最后你只须要将项目打成jar包,上传私服,而后就能够很轻松的使用@Enable带来的便捷了。
@SpringBootApplication
@EnableDisconf(scanPackages="com.demo")
public class Application{
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
复制代码