1、启动器命名规范
1、官方:
- 格式:spring-boot-starter-xxxx
- 样例:spring-boot-starter-web
2、自定义
- 格式:xxxx-spring-boot-starter
- 样例:myself-spring-boot-starter
2、启动器说明
1、启动器其实是两个相互依赖的项目,一个控制版本,一个提供服务。
3、开发步骤
1、建立一个空项目。
2、空项目里建立两个模块(一个自动启动器 一个自动配置类)
1、建立第一个module:myself-spring-boot-starter
2、建立第二个module:myself-spring-boot-starter -autoconfuigure(spring-boot项目)
3、自动配置项目结构
4、开发步骤
1、自动配置类模块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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.demo</groupId>
<artifactId>myself-spring-boot-starter-autoconfigure</artifactId>
<version>1.0-SNAPSHOT</version>
<name>myself-spring-boot-starter-autoconfigure</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2、编写具体业务——>HelloService
package com.demo;
public class HelloService {
//注入配置类
private 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.getSubfix();
}
}
3、编写xxxProperties——>HelloProperties
package com.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "demo.hello")//配置文件前缀
public class HelloProperties {
//注入配置类
private String prefix;
private String subfix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSubfix() {
return subfix;
}
public void setSubfix(String subfix) {
this.subfix = subfix;
}
}
4、编写自动配置类xxxAutoConfiguration——>HelloServiceAutoConfiguration
package com.demo;
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//配置类
@EnableConfigurationProperties(HelloProperties.class)//指定配置文件
@ConditionalOnWebApplication//web应用才生效
public class HelloServiceAutoConfiguration {
//注入绑定Properties配置文件
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
5、将启动类放入META-INF下的spring.factories中
6、启动器里导入自定义的自动配置类
7、将启动器和自动配置安装到本地maven
5、测试自己编写的启动器
1、新建springboot项目
2、导入自定义的启动器
3、编写yaml
4、结果