@EnableAspectJAutoProxy:开启AspectJ(AOP框架)支持。
@EnableAsync:开启异步方法的支持。
@EnableScheduling:开启计划任务的支持。
后续章节:
@EnableWebMvc:开启Web Mvc的支持
@EnableConfigurationProperties:开启@ConfigurationProperties注解配置Bean的支持
@EnableJpaRepositories:开启对Spring Data JPA Repository的支持
@EnableTransactionManagement:开启注解式事务的支持。
@EnableCaching:开启注解式的缓存支持
@Enable*都是通过@import导入配置,来开启一项功能支持,避免开发者使用大量配置代码。
导入配置类的3种方式
1、直接导入:
如:@EnableScheduling
使用@Import(SchedulingConfiguration.class)。SchedulingConfiguration类注解了@Configuration。
2、条件选择配置:
如:@EnableAsync
使用@Import(AsyncConfigurationSelector.class)
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;
}
}
}
查询器:基于@EanableAsync中定义的模式AdviceMode加在@Configuration标记的类上,确定抽象异步配置类的实现类
配置的PROXY,使用ProxyAsyncConfiguration配置类
配置的ASPECTJ,使用ProxyAsyncConfiguration配置类
3、动态注册Bean
如:@EnableAspectJAutoProxy
使用@Import(AspectJAutoProxyRegistrar.class),AspectJAutoProxyRegistrar实现ImportBeanDefinitionRegistrar接口,在运行时自动添加Bean到已有配置类上。
public interface ImportBeanDefinitionRegistrar {
public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry);
}
AnnotationMetadata:获取当前配置类上的注解
BeanDefinitionRegistry:注册Bean