一、自定义starter
说到自定义starter(场景启动器),要先明确以下几点:
- 一是这个场景需要使用什么依赖?
- 二是如何编写springboot的自动配置?
@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,\
- 三是自定义starter的模式
启动器只用来做依赖导入,需要专门提供一个自动配置模块:启动器依赖于自动配置模块,其他项目只需要引入启动器即可。
命名规约(推荐使用以下命名规约):
【1】官方命名空间
– 前缀:“spring-boot-starter-”
– 模式:spring-boot-starter-模块名
– 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
【2】自定义命名空间
– 后缀:“-spring-boot-starter”
– 模式:模块-spring-boot-starter
– 举例:mybatis-spring-boot-starter
二、自定义starter示例
【1】创建一个空的project
【2】在上一步创建的空项目中创建两个Module
【3】配置启动器和启动器配置模块
【3.1】配置启动器配置模块
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();
}
}
该自定义starter可以设置的属性类:
//相当于写了一个属性,用来绑定"zm.hello"的配置
@ConfigurationProperties(prefix = "zm.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;
}
}
使属性类生效的自动配置类:
@Configuration //指明这是一个配置类
@ConditionalOnWebApplication //在web应用中才生效
@EnableConfigurationProperties(HelloProperties.class) //导入属性配置文件
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean //将HelloService加入到容器中
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
在resources
的目录下创建META-INF/spring.factories
注册自动配置类:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zm.starter.springboot.HelloServiceAutoConfiguration
【3.2】配置启动器模块:在pom.xml文件中导入自动配置模块的依赖
<!--启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.zm.starter</groupId>
<artifactId>springboot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
【4】创建测试项目
controller:
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello(){
String sayHello = helloService.sayHello("Make");
return sayHello;
}
}
pom.xml文件的配置:
<!--引入自定义的启动器-->
<dependency>
<groupId>com.zm.starter</groupId>
<artifactId>springboot-zm-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
来源:CSDN
作者:zmt0104
链接:https://blog.csdn.net/qq_40845019/article/details/104523373