1.如何编写自动配置
1.1几大要素
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类顺序
@Bean //给容器添加组件
@ConfigurationProperties //结合相关Properties类绑定相关配置
@EnableConfigurationProperties //让XXXProperties生效加入容器中
//自动配置类要能加载,将需要启动就加载的自动配置类,配置在META_INF/spring.factories
//eg: # 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,\
1.2.模式:
启动器只用来做依赖导入;专门写一个自动i配置模块;启动器依赖自动配置;别人只需要引入启动器
2.Demo
2.1自动配置模块
//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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hw.starter</groupId>
<artifactId>hw-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hw-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter;这是所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
//组件的属性绑定类(用来获取用户定义的配置信息)
package com.hw.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hw.hello")
public class HelloProperties {
//使用属性绑定配置文件配置
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
//提供功能的组件本身
package com.hw.starter;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix()+"====" +name+"===="+helloProperties.getSuffix();
}
}
//组件的配置类(用来将组件加入容器)
package com.hw.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //定义为配置类
@ConditionalOnWebApplication //web应用才有效
@EnableConfigurationProperties(HelloProperties.class) //使HelloProperties属性文件生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
//给容器中加入HelloService,提供别人使用
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
//将属性文件设置进去
service.setHelloProperties(helloProperties);
return service;
}
}
//加载所有自动配置类(位于META-INF下的spring.factories文件中)
# Auto Configure 启动时加载自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hw.starter.HelloServiceAutoConfiguration
2.2自动配置的依赖模块
//只有一个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>
<!-- 启动器,别人只需引用这个启动器的GAV坐标 -->
<groupId>com.hw.starter</groupId>
<artifactId>hw-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 引入自动配置模块 -->
<dependency>
<groupId>com.hw.starter</groupId>
<artifactId>hw-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.3对自定义的stater自动配置进行测试
注意:需要将前面两个模块 install 到 Maven库,测试模块才能引入
//由于在自动配置类中有条件 @ConditionalOnWebApplication //web应用才有效
//存在所以测试项目要引入web依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hw</groupId>
<artifactId>hw-spring-boot-starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hw-spring-boot-starter-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入自定义的starter进行测试-->
<dependency>
<groupId>com.hw.starter</groupId>
<artifactId>hw-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--只有加入web依赖,自定义的starter自动配置才能生效--->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
//测试
package com.hw.hwspringboot;
import com.hw.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHello("我的Starter");
}
}
//application.yml测试自定义配置
//自定义配置
hw.hello:
prefix: hwhwhwh
suffix: HELLO WORLD
//测试结果
hwhwhwh====我的Starter====HELLO WORLD
//改变配置后
hw.hello:
prefix: qwqwqw
suffix: WORLD HELLO
//测试结果
qwqwqw====我的Starter====WORLD HELLO
3.总结
从Demo中不难总结自动配置原理:
1.从MEATA-INF/spring.factories中读取需要进行自动配置的类
2.自动配置类上定义有各种条件,如果条件满足就将提供的功能组件注入到 IOC 容器中
3.功能组件中可配置的属性通过 XXXProperties 类来与yml配置文件进行绑定,达到用户在自己的配置文件中进行配置的目的