1.starter命名规范
官方命名格式:spring-boot-starter-*
spring-boot-starter-test
自定义命名格式:xxx-spring-boot–starter
hello-spring-boot-starter
2.创建maven项目,项目名:hello-spring-boot-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.zhsw</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!--项目打包为UTF-8-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--自定义starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--编译生成spring-configuration-metadata.json给IDEA用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.0.0.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
3.新建properties类
@ConfigurationProperties(prefix = "spring.hello")
public class HelloProperties {
private String name;
private int age;
private String sex;
public HelloProperties(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
@ConfigurationProperties: application.peoperties配置文件可以自动导入properties属性
4.新建service类
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public HelloService() {
}
public HelloService(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(){
return
"大家好,我是"+helloProperties.getName()
+ ",今年"+helloProperties.getAge()+"岁,性别"
+ helloProperties.getSex();
}
}
5.新建config类
//开启配置
@Configuration
//开启使用映射实体对象
@EnableConfigurationProperties(HelloProperties.class)
//当类路径classpath下有指定的类的情况下进行自动配置
@ConditionalOnClass(HelloService.class)
//当配置文件中example.service.enabled=true时进行自动配置,如果没有设置此值就默认使用matchIfMissing对应的值
@ConditionalOnProperty(prefix = "spring.hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfig {
@Autowired
private HelloProperties helloProperties;
@Bean
//当容器(Spring Context)中没有指定Bean的情况下进行自动配置
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
HelloService helloService = new HelloService(helloProperties);
return helloService;
}
}
6.在resources下新建META-INF文件夹,新建spring.factories文件将config进行注册
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhsw.helloWord.HelloServiceAutoConfig
7.执行 mvn clean install 将项目打成jar包发布到本地仓库
hello-spring-boot-starter-1.0-SNAPSHOT.jar
8.新建springboot项目,将hello-spring-boot-starter-1.0-SNAPSHOT.jar坐标引入
<dependency>
<groupId>com.zhsw</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
9.在application.propertiest添加属性
spring.hello.sex=女
spring.hello.age=26
spring.hello.name=铁锤
10.在测试类里进行测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloApplicationTests {
@Autowired
private HelloService helloService;
@Test
public void test() {
System.out.println(helloService.sayHello());
}
}
11.效果如下
注意其中涉及中文乱码问题
源码地址:
自定义项目hello-spring-boot-starter
测试自定义starter项目