现在各种自定义的starter屡见不鲜,也许就需要自己来自定义一个starter了。学习一下starter的构建必备
## starter-定义
首先需要自顶一个配置bean
```
package com.zhao.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(StarterBean.class)
@ConfigurationProperties(prefix = "starterbean")
public class StarterBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StarterBean{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
```
然后需要通过Springboot自动注入的注解,以及Spring spi 的spring.factories将配置bean加入自动配置的管理
```
@Configuration
@ConditionalOnClass
public class StartBeanAutoConfiguration {
static {
System.out.println("custom auto configuration init ");
}
@Bean
public StarterBean starterBean(){
return new StarterBean();
}
}
```
资源目录下新建META-INF文件夹下spring.factories文件,
```
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhao.starter.StartBeanAutoConfiguration
```
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>
<groupId>com.zhao</groupId>
<artifactId>starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
</project>
```
使用maven install之后就可以在新建项目时使用了。
## starter使用测试
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>
<groupId>com.zhao</groupId>
<artifactId>starter-use-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.zhao</groupId>
<artifactId>starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
```
启动类
```
@SpringBootApplication
public class StarterUseApplication {
public static void main(String[] args) {
SpringApplication.run(StarterUseApplication.class,args);
}
}
```
配置starter需要的bean的配置
```
starterbean.id=test123
starterbean.name=zhaozhen
```
测试类
```
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StarterUseApplication.class)
public class StarterUseTest {
@Autowired
StarterBean starterBean;
@Test
public void starUse(){
System.out.println(starterBean);
}
}
```
测试结果,正常使用
![file](http://cdn.kobefan.cn/FgWI2Sss98A9jDw0_Pu4GWE4kTYR)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。