Starter
Starter是SpringBoot一种重要的机制,是一组方便的依赖关系描述符。能够抛弃以前Spring中繁杂的配置,集成了相关的依赖组件,只需引入相关Starter依赖,SpringBoot会自动扫描到要加载的配置信息并启动相应的默认配置。因此,SpringBoot默认在很多企业级开发场景中集成常用的Starter,如:spring-boot-starter-data-jdbc,spring-boot-starter-web
等
为什么要自定义Starter
在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配,简直不要太爽。
自定义Starter命名规则
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用 xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter
工程搭建
1.首先新建一个maven项目,注意该工程名以xxx-spring-boot-starter格式,例如:
2.项目新建完成后,引入相关SpringBoot相关配置依赖,提供包扫描以及自定义配置类功能
<!--springboot配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
核心代码:主要有实体Properties,Service类以及自定义配置类三个
- 实体Properties
/**
* @ConfigurationProperties:指定键值对映射到Java实体类属性
*/
@ConfigurationProperties(prefix = "xf")
public class XfProperties {
private String msg;
private String toWho;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getToWho() {
return toWho;
}
public void setToWho(String toWho) {
this.toWho = toWho;
}
}
- Service类
/**
* Service类,功能具体实现
*/
public class XfService {
private String msg;
private String toWho;
public XfService(String msg, String toWho) {
this.msg = msg;
this.toWho = toWho;
}
public String say(){
return this.msg+","+this.toWho;
}
}
- 配置类
/**
* 配置类,交给IOC容器
* @EnableConfigurationProperties:告诉SpringBoot,让使用@ConfigurationProperties 注解的类生效。
* @ConditionalOnProperty:
* 注解控制 @Configuration是否生效。
* 简单来说也就是我们可以通过在yml配置文件中控制
* @Configuration 注解的配置类是否生效
*/
@Configuration
@EnableConfigurationProperties(XfProperties.class)
@ConditionalOnProperty(prefix = "xf",value = "enabled", matchIfMissing = true)
public class XfConfig {
@Autowired
private XfProperties xfProperties;
@Bean
public XfService xfService(){
return new XfService(xfProperties.getMsg(),xfProperties.getToWho());
}
}
3.SpringBoot在启动时,默认加载并配置classpath下META-INF/spring.factories中的指定的配置类,简单来说,我们只需指定好配置类的类路径,剩下的交给SpringBoot来加载并自动装配。新建META-INF文件夹,创建spring.factories文件 在该文件中加入如下配置,该配置指定 上步骤中定义的配置类为自动装配的配置:
####自动配置自定义Starter
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.nihao.xf.hello.XfConfig
完整目录结构:
打包并引用
在helloworld-spring-boot-starter工程中执行mvn clean install 一个自定义的starter就出来了。可以查看到生成的包放在了maven本地仓库中:
- 新建工程,引入依赖:
<!--自定义starter-->
<dependency>
<groupId>cn.nihao.xf</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- 编辑application.yml:
#测试XfProperties
xf:
to-who: spring
msg: hello
- 测试
@SpringBootApplication
@RestController
public class MyspringbootApplication {
@Autowired
//注入service
XfService xfService;
public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
}
@RequestMapping("/test")
public String index(){
return xfService.say(); //方法调用
}
}
- 浏览器访问
上传到私服Nexus
上面演示的只是在本地使用,假如我们写的代码还不错,为方便将来复用或者提供给他人使用,就可以通过上传到私服,私服搭建请戳我,操作如下:
1.打开私服控制台,指定仓库地址,我这里选择maven-releases存放发布版
2.上传完成后,可以查看到POM依赖,只需引入到其他工程项目即可使用
3.依赖引入
<!--自定义starter-->
<dependency>
<groupId>cn.nihao.xf</groupId>
<artifactId>helloworld-spring-boot-starter</artifactId>
<version>0.0.1.RELEASE</version>
</dependency>
总结
通过自定义SpringBoot的Starter,体会到SpringBoot的启动原理,它其实在背后做了很多准备工作,而用户只需调用main()即可启动程序,总体上省去了繁琐的配置文件,从而转向Java注解开发,这大大减少了开发成本并提升了开发效率。若是某些功能模块需要在多个项目中复用,我们自己来整一个Starter呗,用时只需导依赖,嗯!挺爽的。