本章介绍如何自定义 SpringBoot 的 start
在使用 SpringBoot 的是需要使用那个模块进引入那个模块的 starter 就行, 例如: 需要使用 web 模块, 引入 spring-boot-starter-Web 的 starter 就行. 要使用 jdbc 就引入 spring-boot-starter-jdbc
自定义 starter 理论
1, 自动装配 Bean
自动装配使用配置类 (@Configuration) 结合 Spring4 提供的条件判断注解 @Conditional 及 Spring Boot 的派生注解如 @ConditionOnClass 完成;
2, 配置自动装配 Bean
将标注 @Configuration 的自动配置类, 放在 classpath 下 META- INF/spring.factories 文件中, 如:
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=3 org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,4 org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
3, 自动装配顺序
在特定自动装配 Class 之前 @AutoConfigureBefore
在特定自动装配 Class 之后 @AutoConfigureAfter
指定顺序 @AutoConfigureOrder
4, 启动器(starter)
启动器模块是一个空 JAR 文件, 仅提供辅助性依赖管理, 这些依赖可能用于自动装配或者其他类库
命名规约:
推荐使用以下命名规约
官方命名空间
- 前缀:"spring-boot-starter-"
- 模式: spring-boot-starter - 模块名
- 举例: spring-boot-starter-Web,spring-boot-starter-actuator,spring-boot-starter-jdbc
自定义命名空间
- 后缀:"-spring-boot-starter"
- 模式: 模块 - spring-boot-starter
- 举例: mybatis-spring-boot-starter
总结:
自动配置类
- @Configuration // 指定这个类是一个配置类
- @ConditionalOnXXX // 在指定条件成立的情况下自动配置类生效
- @AutoConfigureAfter // 指定自动配置类的顺序
- @Bean // 给容器中添加组件
@ConfigurationPropertie 结合相关 xxxProperties 类来绑定相关的配置
@EnableConfigurationProperties// 让 xxxProperties 生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类, 配置在 META-INF/spring.factories
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
自定义 starter 定义
1, 在 idea 中新建一个空工程
2, 在空工程中, 新建 2 个模块 hello-springboot-starter 和 hello-springboot-starter-autoconfiger,2 个都是 maven 工程
hello-springboot-starter 依赖 hello-springboot-starter-autoconfiger 工程
目录结构如下:
hello-springboot-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>com.test</groupId>
- <artifactId>hello-springboot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- <!-- 启动器 -->
- <dependencies>
- <!-- 引入自动配置模块 -->
- <dependency>
- <groupId>com.test</groupId>
- <artifactId>hello-springboot-starter-autoconfiger</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
- </project>
pom.xml
hello-springboot-starter-autoconfiger 的 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>com.test</groupId>
- <artifactId>hello-springboot-starter-autoconfiger</artifactId>
- <version>1.0-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.8.RELEASE</version>
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <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>
pom.xml
3, 在 hello-springboot-starter-autoconfiger 工程中, 新建一个 com.test.springboot.starter.HelloProperties 类(属性类)
- package com.test.springboot.starter;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- @ConfigurationProperties(prefix = "test.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;
- }
- }
4, 在 hello-springboot-starter-autoconfiger 工程中, 新建一个 com.test.springboot.starter.HelloService 类(服务类)
- package com.test.springboot.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();
- }
- }
5, 在 hello-springboot-starter-autoconfiger 工程中, 新建一个 com.test.springboot.starter.HelloServiceAutoConfiguration 类(自动配置类)
- package com.test.springboot.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
- // Web 应用才生效
- @ConditionalOnWebApplication
- // 使 HelloProperties 文件生效
- @EnableConfigurationProperties(HelloProperties.class)
- public class HelloServiceAutoConfiguration {
- @Autowired
- HelloProperties helloProperties;
- @Bean
- public HelloService helloService(){
- HelloService helloService = new HelloService();
- helloService.setHelloProperties(helloProperties);
- return helloService;
- }
- }
6, 在 hello-springboot-starter-autoconfiger 工程中, 新建一个 resources/META-INF/spring.factories, 编辑以下内容, 将 HelloServiceAutoConfiguration 类 (自动配置类) 加入到 SpringBoot 自动配置中
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=3 com.test.springboot.starter.HelloServiceAutoConfiguration
自定义 starter 使用
1, 新建一个 SpringBoot Web 工程, 引入自定义的 starter,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.test</groupId>
- <artifactId>test-springboot-start-test</artifactId>
- <version>1.0-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.8.RELEASE</version>
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>com.test</groupId>
- <artifactId>hello-springboot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
2, 编辑测试类
- package com.test.springboot.starter;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class TestApplication {
- @Autowired
- HelloService helloService;
- @Test
- public void context(){
- String str = helloService.sayHello("XXX");
- System.out.println(str);
- }
- }
3, 在 resources/application.YAML 配置文件中加入配置
test:
hello:
prefix: 测试前缀
suffix: 测试后缀
4, 运行测试类, 效果如下:
来源: http://www.bubuko.com/infodetail-3452575.html