目录
spring boot自动配置原理
简介
大家都知道搭建spring boot环境时非常的简单,不像spring那样需要配置很多的XML,也没有引用很多的pom坐标,一个main方法,就能直接启动,所以今天记录下springboot是如何实现的自动配置。
原理 @EnableAutoConfiguration
1、@SpringBootApplication:标注在某个类上说明这个类是SpringBoot的主配置类,Spring Boot需要运行这个类的main方法来启动springboot应用。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2、进入到@SpringBootApplication 注解中
@Target(ElementType.TYPE) //作用域(标记是否可以定义在类、方法、参数上)
@Retention(RetentionPolicy.RUNTIME) //表示该注解什么情况下有效(比如编译期还是运行期)
@Documented//可以使用Java doc 生成文档
@Inherited//如果继承被该注解标注的类,那么该类注解可以被子类使用
@SpringBootConfiguration//标注在某个类上,表示这是一个Spring Boot的配置类
@EnableAutoConfiguration //开启自动配置功能;以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉
//SpringBoot开启自动配置,会帮我们自动去加载 自动配置类
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })//扫描包
public @interface SpringBootApplication {
3、从注解上可以看到@EnableAutoConfigurtation,是开启自动配置的注解,进入@EnableAutoConfiguration注解源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
//省略
}
- @AutoConfigurationPackage:将当前配置类所在包保存在BasePackages的Bean中。以供spring内部使用。
- @Import(AutoConfigurationImportSelector.class):自动配置的关键所在
- 可以看到,在@EnableAutoConfiguration注解内部使用到了@Import注解来完成导入配置的功能,而AutoConfigurationImportSelector实现了DeferredImportSelector接口,内部再解析@Import注解时,会调用getAutoConfigurationEntry方法
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
// 从META‐INF/spring.factories中获得候选的自动配置类
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
//排重
configurations = removeDuplicates(configurations);
//根据EnableAutoConfiguration注解中属性,获取不需要自动装配的类名单
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
// 根据:@EnableAutoConfiguration.exclude
// @EnableAutoConfiguration.excludeName
// spring.autoconfigure.exclude 进行排除
checkExcludedClasses(configurations, exclusions);
// exclusions 也排除
configurations.removeAll(exclusions);
// 通过读取spring.factories 中的OnBeanCondition\OnClassCondition\OnWebApplicationCondition进行过滤
configurations = getConfigurationClassFilter().filter(configurations);
// 这个方法是调用实现了AutoConfigurationImportListener 的bean.. 分别把候选的配置名单,和排除的配置名单传进去做扩展
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
任何一个spring boot应用,都会引入spring-boot-autoconfiguration,而spring.factories文件就在该包下面。spring.factories文件是Key=Value形式,多个Value是使用,隔开,该文件中定义了关于初始化,监听器等信息,而真正使自动配置生效的key是org.springframework.boot.autoconfigure.EnableAutoConfiguration
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\
每一个这样的 xxxAutoConfiguration类都是容器中的一个组件,都加入到容器中;用他们来做自动配置;
@EnableAutoConfiguration注解通过@SpringBootApplication被间接的标记在了Spring Boot的启动类上。在
SpringApplication.run(…)的内部就会执行selectImports()方法,找到所有JavaConfig自动配置类的全限定名对应的
class,然后将所有自动配置类加载到Spring容器中
以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理
@Configuration(proxyBeanMethods = false) //标记了@Configuration Spring底层会给配置创建cglib动态代理。作用:就是防止每次调用本类Bean方法而重新创建对象,Bean是默认单例的
@EnableConfigurationProperties(ServerProperties.class)//将配置文件中对应的值和 ServerProperties绑定起来;
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)//Spring底层@Conditional注解(Spring注解版),根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效; 判断当前应用是否是web应用,如果是,当前配置类生效。
@ConditionalOnClass(CharacterEncodingFilter.class)//判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中进行乱码解决的过滤器
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)//判断配置文件中是否存在某个配置 spring.http.encoding.enabled;如果不存在,判断也是成立
的
public class HttpEncodingAutoConfiguration {
private final Encoding properties;
public HttpEncodingAutoConfiguration(ServerProperties properties) {
this.properties = properties.getServlet().getEncoding();
}
@Bean
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE));
return filter;
}
@Conditional派生注解(Spring注解版原生的@Conditional作用)
作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效:
作用 | 判断是否满足当前指定条件 |
---|---|
@ConditionalOnJava | 系统的java版本是否符合要求 |
@ConditionalOnBean | 容器中存在指定Bean |
@ConditionalOnMissingBean | 容器中不存在指定Bean |
@ConditionalOnExpression | 满足SpEL表达式指定 |
@ConditionalOnClass | 系统中有指定的类 |
@ConditionalOnMissingClass | 系统中没有指定的类 |
@ConditionalOnSingleCandidate | 容器中只有一个指定的Bean,或者这个Bean是首选Bean |
@ConditionalOnProperty | 系统中指定的属性是否有指定的值 |
@ConditionalOnResource | 类路径下是否存在指定资源文件 |
@ConditionalOnWebApplication | 当前是web环境 |
@ConditionalOnNotWebApplication | 当前不是web环境 |
@ConditionalOnJndi | JNDI存在指定项 |
@EnableConfigurationProperties({ServerProperties.class}): 将配置文件中对应的值和 ServerProperties绑定起来;
并把 ServerProperties加入到 IOC 容器中。并注册ConfigurationPropertiesBindingPostProcessor用于将@ConfigurationProperties的类和配置进行绑定
ServerProperties
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
/**
* Server HTTP port.
*/
private Integer port;
/**
* Network address to which the server should bind.
*/
private InetAddress address;
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
/**
* Strategy for handling X-Forwarded-* headers.
*/
private ForwardHeadersStrategy forwardHeadersStrategy;
/**
* Value to use for the Server response header (if empty, no header is sent).
*/
private String serverHeader;
/**
* Maximum size of the HTTP message header.
*/
private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8);
/**
* Type of shutdown that the server will support.
*/
private Shutdown shutdown = Shutdown.IMMEDIATE;
@NestedConfigurationProperty
private Ssl ssl;
@NestedConfigurationProperty
private final Compression compression = new Compression();
ServerProperties通过 @ConfigurationProperties 注解将配置文件与自身属性绑定。
对于@ConfigurationProperties,它的作用就是把全局配置文件中的值绑定到实体类JavaBean上(将配置文件种的值与ServiceProperties绑定起来),而@EnableConfigurationProperties主要是把以绑定值JavaBean加入到IOC容器中。
我们在application.properties声明spring.application.name是通过@ConfigurationProperties注解,绑定到对应的Properties配置类实体上,让后再通过@EnableConfigurationProperties注解导入到spring容器中。
自定义starter
简介
spring boot最强大的功能就是把我们常用的场景启动器抽取成一个个starter(场景启动器),我们通过引入springboot为我们提供的场景启动器,我们再进行少量的配置即可使用对应的功能。即使这样,spring boot也不能涵盖我们需要的场景,往往需要我们自定义starter,来简化spring boot的使用
如何自定义starter
1、我们参考MybatisAutoConfiguration为例,我们看看需要准备哪些东西,下面是WebMvcAutoConfiguration的部分代码:
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(MybatisAutoConfiguration.class);
private final MybatisProperties properties;
private final Interceptor[] interceptors;
private final TypeHandler[] typeHandlers;
private final LanguageDriver[] languageDrivers;
private final ResourceLoader resourceLoader;
private final DatabaseIdProvider databaseIdProvider;
private final List<ConfigurationCustomizer> configurationCustomizers;
我们可以抽取到我们自定义starter时同样需要的一些配置
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
模式:
我们参照spring-boot-start 我们发现其中没有代码:
我们在看它的pom中的依赖中有个 springboot-starter
我们再看看 spring-boot-starter 有个 spring-boot-autoconfigure
关于mybatis一些自动配置都写在了这,所以我们总结。
- 启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库
- 需要再写一个类似spring-boot-configuration的配置模块
- 用的时候只需要引入启动器starter,就可以自动配置了
命名规范:
官方命名:
- 前缀:spring-boot-starter-
- 模式:spring-bootstarter-模块名
- 举例:spring-boot-starter-web
自定义命名空间
- 前缀:-spring-boot-starter
- 模式:模块-spring-boot-starter
- 举例:mybatis-spring-boot-starter
自定义starter实例
1、我们需要先创建一个父maven项目springboot-custom-starter
两个module:mystarter-spring-boot-starter 和 mystarter-spring-boot-autoconfigurer
spring_custom_starter
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_custom_starter</artifactId>
<!--打包方式-->
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mystarter-spring-boot-starter</module>
<module>mystarter-spring-boot-autoconfigure</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<version>2.3.12.RELEASE</version>
<artifactId>spring-boot-starter-parent</artifactId>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<description>自定义spring-boot-starter</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
mystarter-spring-boot-starter
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring_custom_starter</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mystarter-spring-boot-starter</artifactId>
<description>
启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库
</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!--引入 auto configure -->
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>mystarter-spring-boot-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--如果当前starter,还需要其他类库就在这里引用-->
</dependencies>
</project>
</project>
mystarter-spring-boot-autoconfigure:核心
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring_custom_starter</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mystarter-spring-boot-autoconfigure</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
</project>
目录结构
HelloAutoConfigtion.java
package com.starter.mystarter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 卷心菜
* @version 1.0
* @date 2021/10/31 20:39
* @Description
* @motto 路漫漫其修远兮,吾将上下而求索
*/
@Configuration//声明配置类
@ConditionalOnProperty(value = "mystarter.hello.name") //如果配置文件中有该属性才生效
@EnableConfigurationProperties(HelloProperties.class)//通过构造函数传入
public class HelloAutoConfiguration {
@Autowired
HelloProperties helloProperties;
//配置Controller
@Bean
public HelloIndexController helloIndexController(){
return new HelloIndexController(helloProperties);
}
}
HelloIndexController.java
package com.starter.mystarter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 卷心菜
* @version 1.0
* @date 2021/10/31 20:39
* @Description
* @motto 路漫漫其修远兮,吾将上下而求索
*/
@RestController
public class HelloIndexController {
HelloProperties helloProperties;
public HelloIndexController(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
@RequestMapping("/")
public String helloMyStarter(){
return helloProperties.getName()+"成功";
}
}
HelloProperties.java
package com.starter.mystarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author 卷心菜
* @version 1.0
* @date 2021/10/31 20:39
* @Description
* @motto 路漫漫其修远兮,吾将上下而求索
*/
@ConfigurationProperties("mystarter.hello")//绑定properties文件
public class HelloProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.starter.mystarter.HelloAutoConfiguration
使用
目录结构:
application.properties
mystarter.hello.name=customStarter //之前在starter中定义的前缀
启动访问: