Spring Boot的自定义Starter
一般创建自定义Starter时,会分为两个项目:xxx-spring-boot-starter
, xxx-spring-boot-autoconfigure
。starter模块不写任何业务代码,只用来引入autoconfigure模块的依赖,而在其他项目引入该自定义starter时,直接引用starter模块的依赖即可。
下面是编写自定义starter的流程:
-
创建两个模块:
这里以
example-spring-boot-starter
和example-spring-boot-autoconfigure
为例,直接创建Maven项目即可。 -
编写starter模块的配置
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> <artifactId>example-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <groupId>org.example</groupId> <packaging>jar</packaging> <name>example-spring-boot-starter</name> <dependencies> <!--starter模块只需要导入autoconfigure模块的依赖即可--> <dependency> <groupId>org.example</groupId> <artifactId>example-spring-boot-autoconfigure</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
-
编写autoconfigure模块的配置
autoconfigure模块的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"> <groupId>org.example</groupId> <artifactId>example-spring-boot-autoconfigure</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.1.12.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <dependencies> <!-- springboot starter依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 引入该依赖后,可以在编写配置文件时进行提示 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
-
在autoconfigure模块编写对应的具体代码:
-
ExampleAutoConfigure(配置类)
@Configuration // 标注配置类 @EnableConfigurationProperties(ExampleProperties.class) // 在有配置时生效 @ConditionalOnProperty( // 在配置了org.example.enabled属性为true时,配置类才会生效 prefix = "org.example", name = "enabled", havingValue = "true" ) public class ExampleAutoConfigure { @Autowired private ExampleProperties exampleProperties; // 当容器中没有ExampleService时,注入 @ConditionalOnMissingBean(ExampleService.class) @Bean public ExampleService exampleService() { return new ExampleService(exampleProperties); } }
-
ExampleProperties(配置文件对应)
@ConfigurationProperties(prefix = "org.example") // 设置配置文件的前缀 public class ExampleProperties { private String name; private String content; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
-
ExampleService(业务类)
public class ExampleService { private ExampleProperties exampleProperties; public ExampleService() { } public ExampleService(ExampleProperties exampleProperties) { this.exampleProperties = exampleProperties; } public String testExample() { return "ExampleService : " + exampleProperties.getName() + " -- " + exampleProperties.getContent(); } }
-
-
编写spring.factories文件:
注意,这个文件必须在autoconfigure模块的resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.config.ExampleAutoConfigure
-
在其他项目引入进行测试
在其他项目中引入时,只需要直接引入example-spring-boot-starter依赖,在配置文件进行对应的配置后,就可以使用了。
pom文件:
<dependency> <groupId>org.example</groupId> <artifactId>example-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
测试接口:
@RestController public class TestController { @Autowired private ExampleService exampleService; @GetMapping("/print") public String print() { System.out.println(".........."); return exampleService.testExample(); } }
配置文件application.yml
server: port: 7888 org: example: enabled: true name: sprog content: 你好世界
-
访问接口的结果:
自定义Starter已经可以使用了。