神奇的@Enable*注解
在我们日常开发普通的spring-framework项目过程中@Enable用的不是很多,但在SpringBoot开发过程,我们经常会遇到@Enable开始的好多注解,比如@EnableWebMvc、@EnableEurekaServer、@EnableAsync、@EnableScheduling等,今天我们就来分析下这些注解到底是如何工作的,并写手自定的@Enable注解。
@Enable*实现的原理
以@EnableWebMvc为例,先看其源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
可以看出,其本质是使用了@Import,导入的DelegatingWebMvcConfiguration类注解了@Configuration,表明是一个配置类。总结下,@Enable注解其实使用了@Import注解,将需要用到的类注入到容器中,这样我们就可以直接从容器中取出模块锁需要的bean对象使用。
手写自定义的@Enable*注解
1、自定义注解类 EnableMyMvc,此处使用的@Import的ImportSelector的实现类导入
tip:@Import具体使用可翻阅历史博客
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyMvcSelector.class)
public @interface EnableMyMvc {
}
2、上MyMvcSelector和MyMvc类代码
public class MyMvcSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{MyMvc.class.getName()};
}
}
public class MyMvc {
public void doMvc(){
System.out.println("MyMvc doMvc");
}
}
3、看启动类和执行结果
@ComponentScan
@EnableMyMvc
public class SpringApplicationContext {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);
//得到MyMvc类的实例
MyMvc myMvc = context.getBean(MyMvc.class);
myMvc.doMvc();
//关闭容器
context.close();
}
}
从运行结果可知,由于我们在启动类加@EnableMyMvc注解,会将MyMvc类加载到spring容器中。