1.自定义starter的命名规范
官方命名如下图:一般都是spring-boot-starter-*,外部的自定义starter推荐使用*-spring-boot-starter
2.项目结构
如果我们使用就只需要引入下面的就可以了
dependency>
<groupId>com.custom.starter</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> |
custom-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.custom.starter</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.test.custom.starter</groupId>
<artifactId>spring-boot-customstarter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project> |
spring-boot-customstarter-autoconfigure 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.test.custom.starter</groupId>
<artifactId>spring-boot-customstarter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-customstarter-autoconfigure</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
|
3.编写
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(CustomStarterProperties.class)
public class CustomStarterAutoConfiguration {
@Autowired
CustomStarterProperties customStarterProperties;
@Bean
public CustomStarterService getCustomStarterService() {
CustomStarterService customStarterService = new CustomStarterService();
customStarterService.setCustomStarterProperties(customStarterProperties);
return customStarterService;
}
} |
@ConfigurationProperties(prefix = "spring.boot.custom.starter")
public class CustomStarterProperties {
private String param;
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
} |
@Configuration
@ConditionalOnWebApplication //只有在web应用下才可以使用,不懂的可以看spring-boot-autoconfigure中的XXXXXAutoConfiguration中有很多@ConditionalOnXXXXX
@EnableConfigurationProperties(CustomStarterProperties.class)
public class CustomStarterAutoConfiguration {
@Autowired
CustomStarterProperties customStarterProperties;
@Bean
public CustomStarterService getCustomStarterService() {
CustomStarterService customStarterService = new CustomStarterService();
customStarterService.setCustomStarterProperties(customStarterProperties);
return customStarterService;
}
} |
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.custom.starter.CustomStarterAutoConfiguration
|
按照以上编写,一个最简单的自定义starter就写好了,在其他项目导入依赖,然后注入XXXService就可以使用了