springboot-custom starter

时间:2019-03-21
本文章向大家介绍springboot-custom starter,主要包括springboot-custom starter使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring Boot由众多Starter组成,随着版本的推移Starter家族成员也与日俱增。在传统Maven项目中通常将一些层、组件拆分为模块来管理, 以便相互依赖复用,在Spring Boot项目中我们则可以创建自定义Spring Boot Starter来达成该目的。

可以认为starter是一种服务——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息, 由Spring Boot自动通过classpath路径下的类发现需要的Bean,并织入相应的Bean。举个栗子,spring-boot-starter-jdbc这个starter的存在, 使得我们只需要在BookPubApplication下用@Autowired引入DataSource的bean就可以,Spring Boot会自动创建DataSource的实例。

本篇将通过一个简单的例子来演示如何编写自己的starter。

这里讲一下我们的Starter要实现的功能,很简单,提供一个Service,包含一个能够将字符串加上前后缀的方法String wrap(String word)。 而具体的前缀和后缀是通过读取SpringBoot配置文件application.yml而获取的。

添加maven依赖

第一步当然是创建一个maven工程,添加SpringBoot的自动配置的依赖:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.3.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.code</groupId>
12     <artifactId>simple-spring-boot-starter</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>simple-spring-boot-starter</name>
15     <description>springboot-starter</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter</artifactId>
25         </dependency>
26 
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-test</artifactId>
30             <scope>test</scope>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-configuration-processor</artifactId>
36             <optional>true</optional>
37         </dependency>
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-autoconfigure</artifactId>
41         </dependency>
42     </dependencies>
43 
44     <build>
45         <plugins>
46             <plugin>
47                 <groupId>org.springframework.boot</groupId>
48                 <artifactId>spring-boot-maven-plugin</artifactId>
49                 <configuration>
50                     <skip>true</skip>
51                 </configuration>
52             </plugin>
53         </plugins>
54     </build>
55 
56 </project>

注意其中 spring-boot-configuration-processor 的作用是编译时生成spring-configuration-metadata.json, 此文件主要给IDE使用,用于提示使用。如在intellij idea中,当配置此jar相关配置属性在application.yml, 你可以用ctlr+鼠标左键,IDE会跳转到你配置此属性的类中。

这里说下artifactId的命名问题,Spring 官方 Starter通常命名为spring-boot-starter-{name} 如 spring-boot-starter-web

Spring官方建议非官方Starter命名应遵循{name}-spring-boot-starter的格式。

编写Service 

 1 public class ExampleService {
 2 
 3     private String prefix;
 4     private String suffix;
 5 
 6     public ExampleService(String prefix, String suffix) {
 7         this.prefix = prefix;
 8         this.suffix = suffix;
 9     }
10     public String wrap(String word) {
11         return prefix + word + suffix;
12     }
13 }

编写属性类

 1 @ConfigurationProperties("example.service")
 2 public class ExampleServiceProperties {
 3     private String prefix;
 4     private String suffix;
 5 
 6     public String getPrefix() {
 7         return prefix;
 8     }
 9 
10     public void setPrefix(String prefix) {
11         this.prefix = prefix;
12     }
13 
14     public String getSuffix() {
15         return suffix;
16     }
17 
18     public void setSuffix(String suffix) {
19         this.suffix = suffix;
20     }
21 }

编写自动配置类

 1 @Configuration
 2 @ConditionalOnClass(ExampleService.class)
 3 @EnableConfigurationProperties(ExampleServiceProperties.class)
 4 public class ExampleAutoConfigure {
 5 
 6     private final ExampleServiceProperties properties;
 7 
 8     @Autowired
 9     public ExampleAutoConfigure(ExampleServiceProperties properties) {
10         this.properties = properties;
11     }
12 
13     @Bean
14     @ConditionalOnMissingBean
15     @ConditionalOnProperty(prefix = "example.service", value = "enabled",havingValue = "true")
16     ExampleService exampleService (){
17         return  new ExampleService(properties.getPrefix(),properties.getSuffix());
18     }
19 
20 }

解释下用到的几个和Starter相关的注解:

1. @ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。
2. @ConditionalOnMissingBean,当Spring Context中不存在该Bean时。
3. @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true"),当配置文件中example.service.enabled=true时。

添加spring.factories

最后一步,在resources/META-INF/下创建spring.factories文件,内容供参考下面:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.code.starter.config.ExampleAutoConfigure

如果有多个自动配置类,用逗号分隔换行即可。

OK,完事,运行 mvn:install 打包安装,一个Spring Boot Starter便开发完成了。

测试

打包好了当然要测试一下看看了。另外创建一个SpringBoot工程,在maven中引入这个starter依赖, 然后在单元测试中引入这个Service看看效果。

 1 <dependency>
 2        <groupId>com.code</groupId>
 3        <artifactId>simple-spring-boot-starter</artifactId>
 4        <version>0.0.1-SNAPSHOT</version>
 5 </dependency>
 6 <dependency>
 7        <groupId>org.springframework.boot</groupId>
 8        <artifactId>spring-boot-starter-test</artifactId>
 9        <scope>test</scope>
10 </dependency>

修改application.yml配置文件,添加如下内容:

example.service:
  enabled: true
  prefix: prefix
  suffix: suffix

测试类:

 1 @RunWith(SpringRunner.class)
 2 @SpringBootTest
 3 public class ApplicationTests {
 4     @Autowired
 5     private ExampleService exampleService;
 6 
 7     @Test
 8     public void testStarter() {
 9         System.out.println(exampleService.wrap("hello"));
10     }
11 }

运行结果:

prefixhellosuffix

总结

总结下Starter的工作原理:

  1. Spring Boot在启动时扫描项目所依赖的JAR包,寻找包含spring.factories文件的JAR包
  2. 根据spring.factories配置加载AutoConfigure类
  3. 根据 @Conditional 注解的条件,进行自动配置并将Bean注入Spring Context