1:定义starter
1.1:pom
<?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.4.RELEASE</version>
</parent>
<groupId>dongshi.daddy</groupId>
<artifactId>firstspringbootstart</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
1.2:配置类
不管是官方提供的starter还是自定义的starter,使用的时候都需要在配置文件中进行相关的配置,那么这些配置信息在starter中最终会映射到一个类中,因此我们需要首先定义这个类,需要使用到@ConfigurationProperties
注解,该注解源码如下:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
@AliasFor("prefix")
String value() default "";
@AliasFor("value")
String prefix() default "";
boolean ignoreInvalidFields() default false;
boolean ignoreUnknownFields() default true;
}
其中最重要的一个属性是preffix
,该值对应的是我们在配置文件中设置的配置信息的前缀,即需要通过此前缀来进行相关信息的配置,然后类中定义需要配置的属性,则前缀+属性名
就是我们需要在使用starter项目配置文件中配置的信息了,本例定义如下:
/**
* 该类用于封装配置信息
*/
@ConfigurationProperties(prefix = "first")
public class MyFirstConfigurationProperties {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1.3:服务类
服务类,即最终被调用执行具体功能的类,该类和定义普通类没有任何差别,如下:
public class FirstStarterService {
private String name;
private int age;
public FirstStarterService() {}
public FirstStarterService(String name, int age) {
this.name = name;
this.age = age;
}
public void introduceYourself() {
String introduction = "我的名字是:%s,我的年龄是:%s";
System.out.println(String.format(introduction, this.name, this.age));
}
}
1.4:注册服务
通过bean configuration的方式来注册服务,因为需要使用到用户的配置信息所以需要通过@EnableConfigurationProperties
注解来启用配置,并将配置类注入,这里是MyFirstConfigurationProperties
,最后最终要的就是通过@Bean
注解来注册服务类到容器中,这里就是FirstStarterService
,最终代码如下:
@Configuration
@EnableConfigurationProperties(MyFirstConfigurationProperties.class)
public class MyFirstBeanConfig {
@Autowired
private MyFirstConfigurationProperties configurationProperties;
// 注册bean
@Bean(value = "firstStarterService")
public FirstStarterService registerFirstStarterService() {
return new FirstStarterService(configurationProperties.getName(), configurationProperties.getAge());
}
}
1.5:暴露服务
在资源目录下创建META-INF/spring.factories
文件,并将创建的bean configuration类的全限定名定义在里面,这里是dongshi.daddy.beanconfig.MyFirstBeanConfig
,注意每个人不一样,记得更换为自己的。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=dongshi.daddy.beanconfig.MyFirstBeanConfig
1.6:打jar包
mvn clean install
。jar包中配置相关信息在META-INF下,主要有spring-configuration-metadata.json
,该文件内容如下,为该starter可以配置的参数:
{
"groups": [
{
"name": "first",
"type": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties",
"sourceType": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties"
}
],
"properties": [
{
"name": "first.age",
"type": "java.lang.Integer",
"sourceType": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties",
"defaultValue": 0
},
{
"name": "first.name",
"type": "java.lang.String",
"sourceType": "dongshi.daddy.myconfigurationproperties.MyFirstConfigurationProperties"
}
],
"hints": []
}
因此这里可以配置的参数有first.age
,first.name
。还有文件spring.factories
就是我们配置的暴露服务的文件。
2:使用starter
2.1:依赖starter的jar
<dependency>
<groupId>dongshi.daddy</groupId>
<artifactId>firstspringbootstart</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>E:/program_file/maven/maven_rep/dongshi/daddy/firstspringbootstart/1.0-SNAPSHOT/firstspringbootstart-1.0-SNAPSHOT.jar</systemPath>
</dependency>
2.2:添加配置
创建application.properties,并添加配置,如下:
server.port=8082
first.age=30
first.name=dongshidaddy
2.3:定义controller
@RestController
public class FirstSpringBootStarterTestController {
//{"firstStarterService"})
@Resource(name = "firstStarterService")
public FirstStarterService firstStarterService;
@RequestMapping(value = "/firstIntroduce")
public void firstIntroduce(HttpServletResponse response) throws IOException {
firstStarterService.introduceYourself();
response.getWriter().write("test user custom starter");
}
}