工程简介
- 自定义Spring boot starter
1、创建自定义starter项目
- starter-provider,此项目负责封装成自定义starter的jar包,提供给其他项目引用
1.1、添加maven依赖
<modelVersion>4.0.0</modelVersion>
<groupId>com.yubest</groupId>
<artifactId>version-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>version-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
1.2、添加功能代码
//测试用的需要注入的bean
public class Version {
private String version;
private String desc;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getInfo() {
return "Version {version=" + getVersion() + ", desc=" + getDesc() + "}";
}
}
//配置信息
@ConfigurationProperties(prefix = "yubest.version")
public class VersionProperties {
private String version;
private String desc;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
//自动装配
@Configuration
@EnableConfigurationProperties(VersionProperties.class)
public class VersionAutoConfiguration {
@Bean
public Version version(VersionProperties prop) {
Version version = new Version();
version.setVersion(prop.getVersion());
version.setDesc(prop.getDesc());
return version;
}
}
1.3、添加自动装配入口类
- 在
META-INF
目录下添加spring.factories
文件,内容为:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yubest.provider.config.VersionAutoConfiguration
或者通过自定义enable注解的方式:
//自定义的自动装配注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({VersionConfigurationSelector.class})
public @interface EnableVersion {
}
//依赖注入的选择器
public class VersionConfigurationSelector implements ImportSelector {
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{VersionAutoConfiguration.class.getName()};
}
}
- 两种方式,选择其一即可
2、创建自定义starter的引用项目
- starter-consumer 是独立的项目,引用 starter-provider 的功能
2.1、添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.yubest</groupId>
<artifactId>version-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2.2、application.properties中添加配置
yubest.version.version=v1.0.0
yubest.version.desc=添加了自定义starter功能
2.3、添加测试代码
@RestController
public class DemoController {
@Autowired
private Version version;
@GetMapping(value = "/getConfig")
public Object getConfig() {
return version.getInfo();
}
}
@EnableVersion
@SpringBootApplication
public class StarterConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(StarterConsumerApplication.class, args);
}
}
2.4、结果
请求:
curl -X GET \
http://localhost:8080/getConfig
返回:
Version {version=v1.0.0, desc=添加了自定义starter功能}
- 仓库:https://gitee.com/hweiyu/spring-boot-custom-starter