starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦。
虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfiguration。
Starters are a set of convenient dependency descriptors that
you can include in your application.
Starters 是一组可以让你很方便在应用增加的依赖关系描述符的集合。或者可以这样理解,平时我们开发的时候很多情况下都会有一个模块依赖另一个模块,这个时候我们一般都是采用 maven 的模块依赖,进行模块的依赖,但是这种情况下我们完全可以采用 Starter 的方式,将需要被依赖的模块用 Starter 的方式去开发,最后直接引入一个 Starter 也可以达到这样的效果。
命名规则
Do not start your module names with spring-boot . Let’s assume that
you are creating a starter for "acme", name the auto-configure module
acme-spring-boot-autoconfigure and the starter acme-spring-boot-starter.
If you only have one module combining the two, use acme-spring-boot-starter.
由于 SpringBoot 官方本身就会提供很多 Starter,为了区别哪些 Starter 是官方,哪些是私人的或者第三方的,所以 SpringBoot 官方提出,第三方在建立自己的 Starter 的时候命名规则统一用xxx-spring-boot-starter
,而官方提供的 Starter 统一命名方式为spring-boot-starter-xxx
。
举例
假设我们现在有一个需求是将对象转换成 JSON,并在字符串前面加上一个名称,前轴我们支持通过配置文件配置的方式。
1.我们先创建一个 Starter,名字叫myjson-spring-boot-starter
,并加入自动装配和 fastjson 的依赖,如下
2.然后我们创建一个 Service,并增加一个public String objToJson(Object object)
方法,直接调用fastjson
的方法
import com.alibaba.fastjson.JSON;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 18:16<br>
* <b>Desc:</b>无<br>
*/
public class MyJsonService {
private String name;
/**
* 使用 fastjson 将对象转换为 json 字符串输出
*
* @param object 传入的对象
* @return 输出的字符串
*/
public String objToJson(Object object) {
return getName() + JSON.toJSONString(object);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.编写配置类 ,创建一个ConfigurationProperties用于保存你的配置信息
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 18:36<br>
* <b>Desc:</b>无<br>
*/
@ConfigurationProperties(prefix = "ziyou.json")
public class MyJsonProperties {
public static final String DEFAULT_NAME = "ziyou";
private String name = DEFAULT_NAME;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.创建自动化配置类 ,创建一个AutoConfiguration,引用定义好的配置信息;在AutoConfiguration中实现所有starter应该完成的操作,并且把这个类加入spring.factories配置文件中进行声明
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 18:39<br>
* <b>Desc:</b>无<br>
*/
@Configuration
@ConditionalOnClass({MyJsonService.class})//表示如果有后面的类,那么就加载这个自动配置
@EnableConfigurationProperties(MyJsonProperties.class)
public class MyJsonAutoConfiguration {
/**
* 注入属性类
*/
@Autowired
private MyJsonProperties myJsonProperties;
/**
* 当当前上下文中没有 MyJsonService 类时创建类
*
* @return 返回创建的实例
*/
@Bean
@ConditionalOnMissingBean(MyJsonService.class)//如果没有后面的类实例,才自动配置
public MyJsonService myJsonService() {
MyJsonService myJsonService = new MyJsonService();
myJsonService.setName(myJsonProperties.getName());
return myJsonService;
}
}
注意:
@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效。
说明:
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。
5.然后我们再创建 resource/META-INF/spring.factories
文件,增加如下内容,将自动装配的类配置上
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.ziyou.starter.MyJsonAutoConfiguration
6.然后我们通过运行mvn install
命令,将这个项目打包成 jar 部署到本地仓库中,提供让另一个服务调用。
7.创建一个新的 SpringBoot web 项目test-myjson-spring-boot-starter
,提供一个接口去访问。
package com.ziyou.test.controller;
import com.ziyou.starter.MyJsonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 19:04<br>
* <b>Desc:</b>无<br>
*/
@RestController
public class MyJsonController {
@Autowired
private MyJsonService myJsonService;
@RequestMapping(value = "tojson")
public String getStr() {
User user = new User();
user.setName("dsfsf");
user.setAge(18);
return myJsonService.objToJson(user);
}
}
8.application.properties 中配置。
server.port=8089
ziyou.json.name=java-geek-teck
9.pom.xml 中配置刚刚编写的 Starter。
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ziyou</groupId>
<artifactId>test-myjson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.ziyou</groupId>
<artifactId>myjson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
11.启动测试项目,打开浏览器访问接口可以看到如下效果。
从结果中我们可以看到在 Starter 中定义的MyJsonService
已经被成功的调用和执行。