一、starters 原理
1、这个场景需要使用到的依赖是什么?
该场景下需要导入什么依赖。
2、如何编写自动配置(自动装配 Bean)
自动装配使用配置类( @Configuration)结合Spring4 提供的条件判断注解 @Conditional及Spring Boot的派生注解如@ConditionOnClass完成;
参照 WebMvcAutoConfiguration 类:
说明:
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureOrder //指定自动配置类的顺序
@AutoConfigureBefore //在特定自动装配 class 之前
@AutoConfigureAfter //在特定自动装配 class 之后
@Bean //给容器中添加组件
@ConfigurationPropertie //结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
3、配置自动装配(配置自动装配Bean)
自动配置类要能加载将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
配置自动装配Bean:
将标注@Configuration的自动配置类,放在classpath下METAINF/spring.factories文件中,如:
4、命名模式
(1)启动器只用来做依赖导入:启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;
自动配置模块:专门来写一个自动配置模块
启动器依赖自动配置:别人只需要引入启动器(starter)
(2)命名规约
推荐使用以下命名规约:
官方命名空间:
– 前缀:"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、自定义启动器名-spring-boot-starter
二、自定义 starters
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.njf.starter</groupId>
<artifactId>njf-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.njf</groupId>
<artifactId>njf-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
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.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.njf</groupId>
<artifactId>njf-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>njf-spring-boot-starter-autoconfigure</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>
(2)创建 HelloProperties
//绑定可配置的信息
@ConfigurationProperties(prefix = "njf.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;
}
}
(3)创建 HelloService
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();
}
}
(4)创建自动配置类 HelloServiceAutoConfiguration
@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties({HelloProperties.class}) //让属性文件生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
(5)配置自动配置类
在类路径下创建 META-INF/spring.factories 文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.njf.starter.HelloServiceAutoConfiguration
(6)文件目录
3、把上面的两个模块安装到 Maven 仓库
4、创建测试模块
(1)引入自定义 starter 启动器
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入自定义的 starter -->
<dependency>
<groupId>com.njf.starter</groupId>
<artifactId>njf-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2)配置前缀与后缀
njf.hello.prefix=NJF
njf.hello.suffix=HELLO,World!
(3)编写 HelloController 进行测试
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping(value = "/hello")
public String sayHello() {
return helloService.sayHello("Java");
}
}
三、更多 SpringBoot 整合示例
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples