SpringBoot自动化配置的注解以及开关原理是怎样的

介绍

这篇文章给大家介绍SpringBoot自动化配置的注解以及开关原理是怎样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

FreeMarkerAutoConfiguration,这个自动化配置类需要类加载器中的一些类需要存在并且在其他的一些配置类之后进行加载。

SpringBoot的自动化配置原理http://fangjian0423.github.io/2016/06/12/springboot-autoconfig-analysis/

但是还存在一些自动化配置类,它们需要在使用一些注解开关的情况下才会生效,比如spring-boot-starter-batch里的@EnableBatchProcessing注解,@EnableCaching等。

<强>一个需求

在分析这些开关的原理之前,我们来看一个需求:

定义一个注释,让使用了这个Annotaion的应用程序自动化地注入一些类或者做一些底层的事情。

我们会使用弹簧提供的@ import注解配合一个配置类来完成。

我们以一个最简单的例子来完成这个需求:定义一个注解EnableContentService,使用了这个注解的程序会自动注入ContentService这个bean。

@Retention (RetentionPolicy.RUNTIME)

@Target (ElementType.TYPE)

@ import (ContentConfiguration.class)

公共@ interface EnableContentService {}

,

公共接口ContentService {

,,空白doSomething ();

}

,

公共类SimpleContentService实现ContentService {

,,@Override

,,公共空间doSomething () {

,,,,System.out.println(“做一些简单的things");

,,}

}

然后在应用程序的入口加上@EnableContentService注解。

这样的话,ContentService就被注入进来了。SpringBoot也就是用这个完成的。只不过它用了更加高级点的ImportSelector。

<强> ImportSelector的使用

用了ImportSelector之后,我们可以在注释上添加一些属性,然后根据属性的不同加载不同的bean。

我们在@EnableContentService注解添加属性政策,同时导入一个选择器。

@Retention (RetentionPolicy.RUNTIME)

@Target (ElementType.TYPE)

@ Import (ContentImportSelector.class)

公共@ interface EnableContentService {

,,字符串政策()默认“simple"

}

这个ContentImportSelector根据EnableContentService注解里的政策加载不同的bean。

公共类ContentImportSelector实现ImportSelector {

,

,,@Override

,,公共String [] selectImports (AnnotationMetadata importingClassMetadata) {

,,,,Class<?比;annotationType=EnableContentService.class;

,,,,AnnotationAttributes属性=AnnotationAttributes.fromMap (importingClassMetadata.getAnnotationAttributes (

,,,,,,,,annotationType.getName(),假));

,,,,字符串政策=attributes.getString (“policy");

,,,,如果“core" .equals(政策)){

,,,,,,返回新String [] {CoreContentConfiguration.class.getName ()};

,,,,其他}{

,,,,,,返回新String [] {SimpleContentConfiguration.class.getName ()};

,,,,}

,,}

}

CoreContentService和CoreContentConfiguration如下:

公共类CoreContentService实现ContentService {

,,@Override

,,公共空间doSomething () {

,,,,System.out.println(“做一些进口things");

,,}}

,

公共类CoreContentConfiguration {

,,@ bean

,,公共ContentService ContentService () {

,,,,返回新CoreContentService ();

,,}

}

这样的话,如果在@EnableContentService注解的政策中使用核心的话,应用程序会自动加载CoreContentService,否则会加载SimpleContentService。

<强> ImportSelector在SpringBoot中的使用

SpringBoot里的ImportSelector是通过SpringBoot提供的@EnableAutoConfiguration这个注解里完成的。

这个@EnableAutoConfiguration注解可以显式地调用,否则它会在@SpringBootApplication注解中隐式地被调用。

@EnableAutoConfiguration注解中使用EnableAutoConfigurationImportSelector作为ImportSelector。下面这段代码是EnableAutoConfigurationImportSelector中进行选择的具体代码:

@Override

公共String [] selectImports (AnnotationMetadata元数据){

,,尝试{

,,,,AnnotationAttributes属性=getAttributes(元数据);

,,,,List配置=getCandidateConfigurations(元数据,

,,,,,,,,属性),

,,,,配置=removeDuplicates(配置);//删除重复的配置

,,,,Set排除=getExclusions(元数据、属性);//去掉需要排除的配置

,,,,configurations.removeAll(除外);

,,,,配置=排序(配置);//排序

SpringBoot自动化配置的注解以及开关原理是怎样的