什么是Starter?
Spring Boot由众多Starter组成,可以认为Starter是一种服务——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息, 由Spring Boot自动通过classpath路径下的类发现需要的Bean,并加入相应的Bean。
下面通过一个简单的例子来演示如何编写自己的starter。
增加依赖
新建一个SpringBoot项目后,增加如下依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test.starter</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置属性,这个会自动从application.properties下hkis.*获取。
@ConfigurationProperties(prefix = "hkis")
public class DemoConfigProperties {
private String name;
private int age;
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;
}
}
自动配置
@ConditionalOnClass:当classpath下发现该类的情况下进行自动配置。
@ConditionalOnMissingBean:当Spring Context中不存在该Bean时。
@Configuration
@ConditionalOnClass({DemoConfigProperties.class})
@EnableConfigurationProperties(DemoConfigProperties.class)
public class DemoAutoConfiguration {
@Autowired
private DemoConfigProperties demoConfigProperties;
@Bean
@ConditionalOnMissingBean(DemoConfigProperties.class)
public PrintService helloworldService() {
return new PrintService(demoConfigProperties);
}
}
public class PrintService {
private DemoConfigProperties demoConfigProperties;
public PrintService(DemoConfigProperties demoConfigProperties) {
this.demoConfigProperties = demoConfigProperties;
}
public void print(){
System.out.println(demoConfigProperties.getName()+"-"+demoConfigProperties.getAge());
}
}
添加spring.factories
最后,在resources下创建META-INF,META-INF下再增加spring.factories文件。
内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.test.starter.demo.config.DemoAutoConfiguration
然后执行mvn install
打包安装,一个Spring Boot Starter就出炉了,但是这时候可能在生成的jar中包含BOOT-INF目录,就会引起程序包不存在问题。解决方法加入如下配置。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
测试
在另一个工程中引入这个依赖。
<dependency>
<groupId>com.test.starter</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在application.properties加入如下配置,这时候IDEA会自动提示。
hkis.age=12
hkis.name=zhangsan
测试控制层。
@RestController
public class TestController {
@Autowired
PrintService printService;
@GetMapping("/print")
public String index(){
printService.print();
return "test";
}
}
运行后访问这个接口,控制台就会打印如下内容。