Spring Boot 自動配置之@Enable* 與@Import註解

SpringBoot 的自動配置如此強大,好比咱們常常使用的@Enable* 註解來開啓對某方面的支持。那麼@Enable* 註解的原理是什麼呢?html

1、@Enable* 註解與 @Import 註解之間的關係

@Enable* 舉例:java

  • @EnableScheduling 開啓計劃任務的支持
  • @EnableAsync 開啓異步方法的支持
  • @EnableAspectJAutoProxy 開啓對 AspectJ 代理的支持
  • @EnableTransactionManagement 開啓對事務的支持
  • @EnableCaching 開啓對註解式緩存的支持

等等spring

咱們觀察這些@Enable* 的源碼能夠看出,全部@Enable* 註解都是有@Import的組合註解,@Enable* 自動開啓的實現其實就是導入了一些自動配置的Bean緩存

看下 Spring Boot Reference Guide原文app

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 註解的最主要功能就是導入額外的配置信息異步

2、 @Import 註解的用法

官方介紹:ide

* <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}).

有如下三種使用方式spring-boot

一、直接導入配置類(@Configuration 類)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {

}

能夠看到EnableScheduling註解直接導入配置類SchedulingConfiguration,這個類註解了@Configuration,且註冊了一個scheduledAnnotationProcessor的Bean,SchedulingConfiguration的源碼以下:ui

@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();
	}

}

二、依據條件選擇配置類(實現 ImportSelector 接口)

若是並不肯定引入哪一個配置類,須要根據@Import註解所標識的類或者另外一個註解(一般是註解)裏的定義信息選擇配置類的話,用這種方式。.net

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(實現 ImportBeanDefinitionRegistrar 接口)

通常只要用戶確切知道哪些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);
  • AnnotationMetadata 參數用來得到當前配置類上的註解
  • BeanDefinitionRegistry 參數用來註冊Bean

源碼:

@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 也是如此

3、官方文檔

官方文檔

相關文章
相關標籤/搜索