一、starter简介
1、SpringBoot starter机制
SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。
2、为什么要自定义starter
在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可。
3、自定义starter命名规范
官方命名:
-
前缀:spring-boot-starter-xxx
-
比如:spring-boot-starter-web…
自定义命名:
- xxx-spring-boot-starter
- 比如:mybatis-spring-boot-starter
二、starter实现
1、新建工程
这里我命名为shawn-spring-boot-starter
2、添加pom.xml依赖
这里需要注意spring版本,太高可能会导致install失败
<?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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shawn</groupId>
<artifactId>shawn-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shawn-spring-boot-starter</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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
3、定义一个实体类映射配置信息
@ConfigurationProperties(prefix = "shawn.hello")
它可以把相同前缀的配置信息通过配置项名称映射成实体类,可以直接在yml文件进行配置
// 前缀 shawn.hello
@ConfigurationProperties(prefix = "shawn.hello")
public class ShawnProperties {
/**
* 前缀
*/
private String prefix;
/**
* 后缀
*/
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
4、编写自己的服务
public class ShawnService {
ShawnProperties ShawnProperties;
public ShawnProperties getShawnProperties() {
return ShawnProperties;
}
public void setShawnProperties(ShawnProperties ShawnProperties) {
this.ShawnProperties = ShawnProperties;
}
public String sayHello(String name){
return ShawnProperties.getPrefix() + name + ShawnProperties.getSuffix();
}
}
5、定义配置类
这里,我们将ShawnService类定义为一个Bean,交给Ioc容器。
@Configuration
配置注解
@EnableConfigurationProperties
。该注解是用来开启对@ConfigurationProperties
注解配置Bean的支持。当然了,也可以在 @ConfigurationProperties
注解的类上添加 @Configuration
或者 @Component
注解
@Configuration
@ConditionalOnWebApplication //web应用生效
@EnableConfigurationProperties(ShawnProperties.class)
public class ShawnServiceAutoConfiguration {
@Autowired
ShawnProperties shawnProperties;
@Bean
public ShawnService shawnService(){
ShawnService service = new ShawnService();
service.setShawnProperties(shawnProperties);
return service;
}
}
6、在resources
编写一个自己的 META-INF\spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.shawn.ShawnServiceAutoConfiguration
7、打包到Maven仓库
三、测试starter
1、新建项目,引入依赖
<dependency>
<groupId>com.shawn</groupId>
<artifactId>shawn-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2、编写测试类
@RestController
public class ShawnCOntroller {
@Autowired
ShawnService shawnService;
@GetMapping("/hello")
public String hello(){
return shawnService.sayHello("shawn");
}
}
3、配置
在application.yml
中进行配置
shawn:
hello:
prefix: prefix
suffix: suffix
4、测试
成功打印
参考:
https://www.cnblogs.com/hellokuangshen/p/12486631.html
https://www.cnblogs.com/hello-shf/p/10864977.html