
简介

在了解了
Spring Boot的自动装配原理后,我们来实现一个Spring Boot常见的Starter项目
准备工作summer-boot-hello-starter 定义申明自动装配配置类Starter 定义申明编写测试项目总结更多
手机用户请
横屏获取最佳阅读体验,REFERENCES中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。
平台 | 地址 |
|---|---|
CSDN | https://blog.csdn.net/sinat_28690417 |
简书 | https://www.jianshu.com/u/3032cc862300 |
个人博客 | https://yiyuery.github.io/NoteBooks/ |
正文
依赖和环境
项目结构

.
首先,需要定义自动装配的配置,我们期望可以将starter和yml中配置的一些字段绑定起来,在引入对应starter后,会读取配置并完成对应Spring实例的自动装配
自动装配配置类
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloSummerBootService.class)
@ConditionalOnProperty(prefix = "summer.boot.hello",value="enabled",matchIfMissing = true)
@Slf4j
public class HelloAutoConfiguration {
@Bean
@ConditionalOnMissingBean(HelloSummerBootService.class)
public HelloSummerBootService helloWorldService(HelloProperties helloProperties){
log.info("HelloAutoConfiguration >> register HelloSummerBootService has started!");
return HelloSummerBootService.builder().name(helloProperties.getName()).message(helloProperties.getMessage()).build();
}
}
yml配置加载类
@ConfigurationProperties(prefix="summer.boot.hello")
@Component
@Getter
@Setter
public class HelloProperties {
private String name;
private String message;
}
prefix 对应值的yml中的属性
Spring实例申明
@Getter
@Setter
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class HelloSummerBootService {
private String name;
private String message;
/**
* hello方法输出配置内容
*
* @return
*/
public String hello() {
return name + "," + message + " > HelloSummerBootService!";
}
}
自动装配类申明
summer-boot-autoconfigure/src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.summer.boot.autoconfigure.hello.HelloAutoConfiguration
项目依赖
此处由于申明了父级module的依赖,用于公共依赖,如果是简单项目,直接引用org.springframework.boot:spring-boot-starter:2.1.0-REALEASE即可
//summer-boot-autoconfigure
dependencies {
compile project(":summer-boot:summer-boot-project:summer-boot-parent")
}
//summer-boot-parent
dependencies {
compile libs["spring-boot-starter"] //对应org.springframework.boot:spring-boot-starter:2.1.0-REALEASE
}

.
dependencies {
//引入starter父级module
compile project(":summer-boot:summer-boot-project:summer-boot-starters:summer-boot-starter")
compile libs["spring-boot-starter-web"]
}
//starter父级module,引入前面实现的自动装配配置module
dependencies {
compile project(":summer-boot:summer-boot-project:summer-boot-autoconfigure")
}
由于前文引入了spring-boot-starter-web,我们直接编写个简单的Demo Web工程进行测试

.
WEB 接口定义
@RestController
public class HelloController {
//注入自动装配的实现类
@Resource
HelloSummerBootService helloSummerBootService;
@GetMapping("hello")
public String hello(){
return helloSummerBootService.hello();
}
}
项目的依赖就被大大简化了
dependencies {
compile project(":summer-boot:summer-boot-project:summer-boot-starters:summer-boot-hello-starter")
}
由于我们在autoconfigure项目中针对这个
Starter申明了从yml读取配置,我们在yml中补充下配置
summer:
boot:
hello:
name: Ashe
message: You Have Actived Summer Boot Hello Starter!
enabled: true
server:
port: 8081
启动效果

.

.
本文介绍了如何实现一个基于Spring Boot的Starter,其实原理比较简单,用到的就是Spring Boot的自动装配能力,不清楚的小伙伴可以看前几期的推文。
大概步骤此处再强调下:
autoconfigure项目,并利用条件装配编写配置类spring.factoriesStarter默认的实现类。拓展
能做什么?
Starter,可以实现快速提供支持,避免重复开发Swagger,完全可以根据业务需求,实现一个自动化装配的实现类,通过yml去定义扫描的位置和需要进行的接口分组,其余的直接自动实现装配和扫描(目前已经有人实现了,后续推文小编也会实现一些工具的自动装配Starter).