源码地址:https://github.com/877148107/springboot_integrate
-
参考WebMvcAutoConfiguration
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {
1)、自动配置的基本特征
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
2)、starter的模式
- 启动器只是用来依赖导入的
- 需要一个专门的自动配置模块来处理自动配置的相关业务
- 其他项目只需要引入启动器starter
-
新建一个SpringBoot项目
1)、自动配置项目AutoConfiguration
2)、 新建项目目录
3)、自定义配置文件实体
@ConfigurationProperties(prefix = "spring.custom")
public class CustomProperties {
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;
}
}
4)、业务处理
public class CustomService {
CustomProperties customProperties;
public CustomProperties getCustomProperties() {
return customProperties;
}
public void setCustomProperties(CustomProperties customProperties) {
this.customProperties = customProperties;
}
public String sayHello(String str){
return customProperties.getPrefix()+"_"+str+"_"+customProperties.getSuffix();
}
}
5)、自动配置类
//配置类
@Configuration
//web环境才生效
@ConditionalOnWebApplication
//属性配置类生效
@EnableConfigurationProperties(CustomProperties.class)
public class CustomServiceAutoConfiguration {
@Autowired
CustomProperties customProperties;
@Bean
public CustomService customService(){
CustomService customService = new CustomService();
customService.setCustomProperties(customProperties);
return customService;
}
}
6)、/META-INF/spring.factories配置上自动配置类这样在启动的时候才会加载这个自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wmy.integrate.starter.CustomServiceAutoConfiguration
7)、POM文件
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wmy.integrate.starter</groupId>
<artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>custom-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入SpringBoot starter,所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
-
新建一个Maven项目
1)、Maven只是用作引入自动配置的依赖
<?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>springboot_integrate</artifactId>
<groupId>com.wmy.integrate</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.wmy.integrate.custom.starter</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<dependencies>
<!--引入自定义自动配置-->
<dependency>
<groupId>com.wmy.integrate.starter</groupId>
<artifactId>custom-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
-
将两个项目打包到maven库
-
新建一个SpringBoot测试项目
来测试自动配置的starter是否生效
1)、引入maven依赖
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wmy.integrate.project</groupId>
<artifactId>springboot-integrate-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-integrate-project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入自定义的starter-->
<dependency>
<groupId>com.wmy.integrate.custom.starter</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)、新建一个controller测试
@RestController
public class CustomController {
@Autowired
CustomService customService;
@GetMapping("/hello")
public String sayHello(){
return customService.sayHello("自定义Starter");
}
}
3)、配置实体属性application.properties
spring.custom.prefix=测试
spring.custom.suffix=Hello world