#如何自定义Starters#
### 1.创建思路 ###
1.这个场景需要用到的依赖是什么?
2.如何编写自动配置,一些注解的功能?
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
3.starter的模式:
* 启动器只是用来做依赖导入
* 自动配置模块用来写代码逻辑
* 启动器依赖自动配置,只需要引入启动器会自动引入自动配置
* 自定义启动器名:启动器名-spring-boot-starter
### 2.创建流程 ###
1.创建一个自动配置项目:zhangbao-spring-boot-starter-autoconfigure,在pom.xml文件中导入以下依赖
<!‐‐引入spring‐boot‐starter;所有starter的基本配置‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 使用ConfigurationProperties需要导入的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.创建一个maven项目:zhangbao-spring-boot-starter,只在pom.xml文件中导入自动配置项目的依赖
<!-- 引入自动配置模块 -->
<dependency>
<groupId>com.zhangbao</groupId>
<artifactId>zhangbao-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
3.编写配置类:HelloProperties
//data注解是lombok插件的注解用来生成属性的get/set方法
@Data
@ConfigurationProperties(prefix = "spring.zhangbao.hello")
public class HelloProperties {
private String prefix;
private String suffix;
}
4.编写业务逻辑类
@Data
public class HelloService {
private HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
}
}
5.编写自动配置类
@Configuration
//web环境该配置类才生效
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfigure {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
6.将自动配置类配置在META‐INF/spring.factories文件中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhangbao.config.HelloAutoConfigure
7.使用maven install命令先将zhangbao-spring-boot-starter-autoconfigure项目打包到仓库中
8.然后再使用maven install命令将zhangbao-spring-boot-starter项目打包到仓库中(由于该项目依赖zhangbao-spring-boot-starter-autoconfigure项目所以先将zhangbao-spring-boot-starter-autoconfigure项目打包到仓库中)
9.创建spring-boot-custom-test项目用来测试starter是否创建成功,由于步骤5中自动配置类是在web环境中才生效,所以创建的项目需要导入web依赖
10.测试项目导入zhangbao-spring-boot-starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 导入zhangbao-spring-boot-starter依赖 -->
<dependency>
<groupId>com.zhangbao</groupId>
<artifactId>zhangbao-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
11.创建测试Controller
@RestController
public class HelloController {
//此时注入的就是zhangbao-spring-boot-starter-autoconfigure项目中的HelloService
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello(String name) {
return helloService.sayHello(name);
}
}
12.在application.yml配置文件中添加步骤3中HelloProperties配置类的prefix和suffix
spring:
zhangbao:
hello:
prefix: hello
suffix: welcome
13.启动spring-boot-custom-test项目,在浏览器中输入localhost:8080/hello?name=zhangbao
正常应该显示:hello-zhangbao-welcome
14.自定义Spring Boot Starter成功
15.详细代码见spring-boot-custom文件夹
SpringBoot自定义Starters
最新推荐文章于 2024-05-12 23:38:31 发布