SpringBoot 的自动配置如此强大,好比咱们常常使用的@Enable* 注解来开启对某方面的支持。那么@Enable* 注解的原理是什么呢?html
@Enable* 举例:java
等等spring
咱们观察这些@Enable* 的源码能够看出,全部@Enable* 注解都是有@Import的组合注解,@Enable* 自动开启的实现其实就是导入了一些自动配置的Bean缓存
看下 Spring Boot Reference Guide原文bash
You need not put all your @Configuration into a single class. The @Import annotation
can be used to import additional configuration classes.
您不须要把全部的 @Configuration 放到一个类中。@Import 注解能够导入额外的配置类。
复制代码
@Import 注解的最主要功能就是导入额外的配置信息app
官方介绍:异步
* <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
* Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
* {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
* classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
复制代码
有如下三种使用方式ide
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {
}
复制代码
能够看到EnableScheduling注解直接导入配置类SchedulingConfiguration,这个类注解了@Configuration,且注册了一个scheduledAnnotationProcessor的Bean,SchedulingConfiguration的源码以下:spring-boot
@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();
}
}
复制代码
若是并不肯定引入哪一个配置类,须要根据@Import注解所标识的类或者另外一个注解(一般是注解)里的定义信息选择配置类的话,用这种方式。ui
ImportSelector接口只有一个方法
String[] selectImports(AnnotationMetadata importingClassMetadata);
复制代码
AnnotationMetadata:用来得到当前配置类上的注解
例:
@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继承AdviceModeImportSelector,AdviceModeImportSelector类实现ImportSelector接口 根据AdviceMode的不一样来选择生明不一样的Bean
public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {
private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
"org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";
@Override
@Nullable
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;
}
}
}
复制代码
通常只要用户确切知道哪些Bean须要放入容器的话,本身能够经过spring 提供的注解来标识就能够了,好比@Component,@Service,@Repository,@Bean等。 若是是不肯定的类,或者不是spring专用的,因此并不想用spring的注解进行侵入式标识,那么就能够经过@Import注解,实现ImportBeanDefinitionRegistrar接口来动态注册Bean。 好比:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
复制代码
AspectJAutoProxyRegistrar实现了ImportBeanDefinitionRegistrar接口,ImportBeanDefinitionRegistrar的做用是在运行时自动添加Bean到已有的配置类,经过重写方法:
public void registerBeanDefinitions( AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
复制代码
源码:
@Override
public void registerBeanDefinitions( AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
AnnotationAttributes enableAspectJAutoProxy =
AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy != null) {
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
}
}
复制代码
Mybatis 中大名鼎鼎的@MapperScan 也是如此