SpringBoot自动配置原理
SpringBoot自动配置原理(SpringBoot自动装配原理,SpringBoot starter原理)
SpringBoot可以根据定义在classpath下的类,自动的给你生成一些Bean,并加载到Spring的Context中,自动配置充分的利用了Spring 4.0的条件化配置特性,能够自动配置特定的Spring bean,用来启动某项特性;
关于条件化@Conditional注解:
如果你希望一个bean在某些条件下加载,在某些条件下不加载,则可以使用@Conditional注解;
@Configuration
public class MyConfig {
@Bean
@Conditional(MyBeanCondition.class)
public MyBean myBean(){
return new MyBean();
}
}
public class MyBeanCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false;
}
}
当@Conditional(MyBeanCondition.class)为true时,MyBean才会被创建,否则不会创建;
有了条件注解以保证某些bean在没满足特定条件的情况下就可以不必初始化,避免在bean初始化过程中由于条件不足,导致应用启动失败。
Conditional:有条件的; 条件; 有条件; 条件响应; 条件式;
01、ConditionalOnBean 当spring容器中有某个bean时
02、ConditionalOnClass 当有某个class时
03、ConditionalOnMissingBean 当没有某个bean时
04、ConditionalOnMissingClass
05、ConditionalOnCloudPlatform
06、ConditionalOnExpression
07、ConditionalOnJava
08、ConditionalOnJndi
09、ConditionalOnNotWebApplication
10、ConditionalOnProperty
11、ConditionalOnResource
12、ConditionalOnSingleCandidate
13、ConditionalOnWebApplication
14、ConditionalOnRepositoryType
15、ConditionalOnMissingFilterBean
16、ConditionalOnEnabledResourceChain
在编写SpringBoot项目时,入口类上都会使用@SpringBootApplication注解了,我们可以看一下源代码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
//这些是SpringBootApplication 注解的变量
//exclude : 排除指定的类
//@SpringBootApplication(exclude = RedisAutoConfiguration.class)可以排除自定义配置等等,根据自己的需要进行定制。
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
//excludeName:排除指定的类名,通过bean name来进行排除指定的类,如下:
//@SpringBootApplication(excludeName = "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration")
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};
//scanBasePackages: 扫描指定的包添加到Spring容器中,参数为数组类型:
//@SpringBootApplication(scanBasePackages="com.bjpowernode.boot.component")
//扫描的包能注册识别,没有扫描的包将不能注册识别;
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
//scanBasePackageClasses: 扫描指定包的类并添加到Spring容器中,参数为Class类型数组格式:
//@SpringBootApplication(scanBasePackageClasses=MyComponent.class)
//注册的类能识别,在同级包下或子包下的都能注册,否则不能识别
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "nameGenerator")
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
boolean proxyBeanMethods() default true;
}
@SpringBootApplication注解本身又是一个复合注解,它等效于如下四个注解:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@ConfigurationPropertiesScan
@SpringBootConfiguration
该注解等效于@Configuration,那也就是说这个注解相当于@Configuration,所以这两个注解作用是一样的,它是让我们能够去注册一些额外的Bean,或者导入一些额外的配置,@Configuration还表示该类是一个配置类,不需要额外的xml进行配置,同时该类也是Spring容器中的一个bean。
@EnableAutoConfiguration (重点)
该注解是Spring Boot自动配置注解,Spring Boot中的自动配置主要是@EnableAutoConfiguration的功劳,该注解可以让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
该注解由如下两个注解构成:
1.@AutoConfigurationPackage
让包中的类以及子包中的类能够被自动扫描到Spring容器中;
2.@Import(EnableAutoConfigurationImportSelector.class)
这个注解就是通过import的方式将EnableAutoConfigurationImportSelector添加到Spring容器中;
EnableAutoConfigurationImportSelector 实现自动化配置导入
Spring框架本身也提供了几个名字为@Enable开头的Annotation定义。比如@EnableScheduling、@EnableCaching等,@EnableAutoConfiguration的理念和这些注解其实是一脉相承的。
@EnableScheduling是通过@Import将Spring调度框架相关的bean定义都加载到IoC容器。
@EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器;
该注解通过@Import注解导入了一个组件:AutoConfigurationImportSelector,该组件实现了接口:
public interface DeferredImportSelector extends ImportSelector
该接口主要是为了导入@Configuration的配置项,而DeferredImportSelector是延期导入,当所有的@Configuration都处理过后才会执行;
导入AutoConfigurationImportSelector类,会执行到它的process方法,如下:
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {
...............................................................................
private static class AutoConfigurationGroup
implements DeferredImportSelector.Group, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware {
@Override
public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,
() -> String.format("Only %s implementations are supported, got %s",
AutoConfigurationImportSelector.class.getSimpleName(),
deferredImportSelector.getClass().getName()));
//getAutoConfigurationMetadata() 获取自动化配置的元数据
AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
.getAutoConfigurationEntry(getAutoConfigurationMetadata(), annotationMetadata);
this.autoConfigurationEntries.add(autoConfigurationEntry);
for (String importClassName : autoConfigurationEntry.getConfigurations()) {
this.entries.putIfAbsent(importClassName, annotationMetadata);
}
}
//获取自动化配置元数据
//AutoConfigurationMetadataLoader.loadMetadata 通过JDK Properties.load(inputStream) 来加载META-INF/spring-autoconfigure-metadata.properties中配置的需要自动加载的类,并最后封装成PropertiesAutoConfigurationMetadata对象,将加载好的properties信息赋值其properties属性
private AutoConfigurationMetadata getAutoConfigurationMetadata() {
if (this.autoConfigurationMetadata == null) {
this.autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
}
return this.autoConfigurationMetadata;
}
}
}
META-INF/spring-autoconfigure-metadata.properties中配置项,如下:
它的意思同@ConditionalOnClass注解,key为权限类名, value为key所依赖的其他Class,如果value不存在,则key所代表的类不会自动装配
接下来((AutoConfigurationImportSelector) deferredImportSelector).getAutoConfigurationEntry方法:
protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,
AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
//获取注解@EnableAutoConfiguration的属性,即exclude,excludeName
AnnotationAttributes attributes = getAttributes(annotationMetadata);
//加载文件中配置的自动化配置META-INF/spring.factories
// loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList()); 中getOrDefault 取出org.springframework.boot.autoconfigure.EnableAutoConfiguration为key的value
//Enumeration<URL> urls =classLoader.getResources("META-INF/spring.factories")
//URL url = urls.nextElement();
//UrlResource resource = new UrlResource(url);
//InputStream is = resource.getInputStream();
//Properties.load(is)
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
//去重
configurations = removeDuplicates(configurations);
//获取注解上的排除掉的类
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
//删除掉排除 项
configurations.removeAll(exclusions);
//过滤,过滤掉那些,还不满足加载条件的类,即类的@Conditional的条件没满足,则不加载,去除掉
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
configurations = filter(configurations, autoConfigurationMetadata);的大概逻辑如下:
META-INF/spring.factories中配置的自动配置类(org.springframework.boot.autoconfigure.EnableAutoConfiguration的 value类),这些类会被作为自动装配的候选类,接下来会根据META-INF/spring-autoconfigure-metadata.properties这个中配置的类与依赖类的对应关系,去校验该类所依赖的Class(ConditionalOnClass)是否存在(拿到className,通过Class.forName方法,不抛异常就说明该class存在),如果存在,则该类可以被自动装配.
另外,这个地放比较耗时间,springboot将数据一分为二,并另起了一个线程,与主线程一起执行校验,如下代码所示之处.
private ThreadedOutcomesResolver(OutcomesResolver outcomesResolver) {
this.thread = new Thread(() -> this.outcomes = outcomesResolver.resolveOutcomes());
this.thread.start();
}
@EnableAutoConfiguration与@Conditional
@EnableAutoConfiguration自动加载配置,@Conditional根据环境决定是否解析处理配置,这两个注解的配合完成了自动化配置功能;
在Spring Boot中怎么自定义自动配置?
1、需要提供jar包,在jar包中需要包含META-INF/spring.factories文件;
2、在spring.factories中添加配置:
org.springframework.boot.autoconfigure.EnableAutoConfiguration =
com.bjpowernode.xxxx.xxxConfiguration(该类我们自己去实现,里面主要是要做自动化的配置,给使用者把该配的配置好,让别人可以直接用)
3、xxxConfiguration的实现需要添加注解@Configuration;
4、xxxConfiguration也可以选择添加@Conditional来适应不同的环境;
5、在xxxConfiguration类中实现自动化配置;
有了SpringBoot的自动化配置,我们可以灵活的自定义我们自己的自动配置,当应用需要该功能时,只需要简单的依赖该jar包即可,同时Spring Boot为我们提供的条件注解,同样的代码可以灵活适应各种环境;