SpringBoot的核心就是自动配置,自动配置又是基于条件判断来配置Bean。本文将会追踪源码,一步一步讲解SpringBoot的自动配置原理。
文章目录
一、Spring Boot自动配置
Spring Boot最大的特点就是省略了繁杂的配置工作。以前我们使用Spring 、Spring MVC的时候,需要配置web.xml、application.xml等一些配置文件,来加载一些我们需要的组件,比如Spring整合Mybatis,我们需要手动配置大量的配置信息。而使用Spring Boot帮我们省去大量繁杂的配置工作,我们的工作就是在pom.xml中添加Mybatis的场景依赖(mybatis-spring-boot-starter),在application.yml文件中设置数据数据库的地址、用户名以及其他的设置即可。
二、Spring Boot自动配置原理思维导图
三、主程序入口、启动类
Spring Boot项目的都有一个启动类,该类一般以XXXAppApplication命名,并且需要有@SpringBootApplication注解,表是这是一个启动类。雷必须包含main()方法,所以,Spring Boot项目可以直接从这个类启动项目,右键,run main或者debug main即可启动。
@SpringBootApplication
public class OjAppApplication {
public static void main(String []args){
SpringApplication.run(OjAppApplication.class, args);
}
}
3.1 @SpringBootApplication 注解
这是一个复合注解,自动配置就是从这里开始处理,其中最主要的是@EnableAutoConfiguration注解。
@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}
)}
)
@SpringBootConfiguration:这个注解表明这个类是一个SpringBoot配置类,他底层下边使用@Configuration注解。
@EnableAutoConfiguration:这个注解表明开启自动配置,也是自动配置的关键。
3.2 @EnableAutoConfiguration注解
这是一个复合注解。
@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 {};
}
@AutoConfigurationPackage:这个注解帮我们获取到当前类所在的包路劲,这个包名最终会保存在一个叫做BasePackages的been中。这个路劲将会作为Spring Boot管理Been的package进行使用。也就是说,Spring Boot内部最终会扫描这个包下的所有的类,并且将这些类注册为Been,交给Spring IOC容器来管理。
@Import({AutoConfigurationImportSelector.class}):这个注解帮我们导入了自动配置的类,该类实现了加载相应的场景下的所有的需要自定配置类,并交给Spring IOC容器管理。
3.3 AutoConfigurationImportSelector.class源码
AutoConfigurationImportSelector implements DeferredImportSelector该类实现了DeferredImportSelector类,并重写了getImportGroup()方法并返回了一个实现Group类的AutoConfigurationGroup类,AutoConfigurationGroup该类中有两个方法,process()和selectImports()方法。
- process():首先会调用process()方法,获取到META-INF/spring.factories该文件中所有属于自动配置的全限定名的类信息。保存格式为map,其中key为META-INF/spring.factories该文件中所有属于自动配置的类的全限定名,value为当前配置类信息,所有的value都是一样的。
- selectImports():其次会调用selectImports()方,该方法会对这些自动配置类进行过滤和排序操作。最中得正真需要的自动配置类的信息返回。最中被注册为Been。
META-INF/spring.factories文件中的内容:
四、以ServletWebServerFactoryAutoConfiguration为例
ServletWebServerFactoryAutoConfiguration 就是嵌入式servlet容器的自动配置类。
@Configuration(
proxyBeanMethods = false
)
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
public ServletWebServerFactoryAutoConfiguration() {
}
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties, ObjectProvider<WebListenerRegistrar> webListenerRegistrars) {
return new ServletWebServerFactoryCustomizer(serverProperties, (List)webListenerRegistrars.orderedStream().collect(Collectors.toList()));
}
@Bean
@ConditionalOnClass(
name = {"org.apache.catalina.startup.Tomcat"}
)
****省略其他代码*****
}
@EnableConfigurationProperties({ServerProperties.class}):ServletWebServerFactoryAutoConfiguration最终调用这个ServerProperties.class使得配置生效。
@ConditionalOnWebApplication(type = Type.SERVLET):当应用是一个web应用时才生效。
@EnableConfigurationProperties({ServerProperties.class}):配置文件中配置的相应的配置属性加载到名为ServerProperties的bean中。
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class}):这里三种servlet容器:Tomcat,Jetty,Undertow,只有当对应容器的类在classpa上时,才会去初始化对应的容器。比如:“org.apache.catalina.startup.Tomcat”会去加载Tomcat容器。
4.1 ServerProperties.class
配置在配置文件application.yml中相关的以server开头的信息会被加载到这been个中。
@ConfigurationProperties(
prefix = "server",
ignoreUnknownFields = true
)
public class ServerProperties {
private Integer port;
private InetAddress address;
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
private ServerProperties.ForwardHeadersStrategy forwardHeadersStrategy;
private String serverHeader;
private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8L);
private Shutdown shutdown;
@NestedConfigurationProperty
private Ssl ssl;
@NestedConfigurationProperty
private final Compression compression;
@NestedConfigurationProperty
private final Http2 http2;
private final ServerProperties.Servlet servlet;
private final ServerProperties.Tomcat tomcat;
private final ServerProperties.Jetty jetty;
private final ServerProperties.Netty netty;
private final ServerProperties.Undertow undertow;
****其他代码省略*****
}
这个类中有一个熟悉的注解@ConfigurationProperties(prefix = “server”,ignoreUnknownFields = true),这个属性就是说,将配置在配置文件application.yml中相关的以server开头的信息会被加载到这个been中,比如常规的设置server.port=8081,这个设置服务端口的信息就会被设置到这个been中并生效,如果不设置,那么就会使用Tomcat的默认端口8080。
总结
本文主要是讲解了SprinBoot自动配置的原理,包括一些源码的解读,讲解了一些场景下的自动配置类是是如何加载到Spring IOC容器中的。