步骤
- 创建Stater 项目
- 定义Starter 需要的配置类
- 编写自动配置类
- 编写spring.factories 文件加载自动配置类
- 编写配置提示文件spring-configuration-metadata.json (非必需)
示例
使用IDEA 新建一个Maven 项目。
pom.xml:
1 |
|
目录文件:
配置参数类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package cn.vgbhfive.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @time: 2019/08/13
* @author: Vgbh
*/
// 配置参数前缀
public class DemoProperties {
private String name; // 配置参数
}自动配置类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package cn.vgbhfive.config;
import cn.vgbhfive.client.VgbhfiveClient;
import cn.vgbhfive.properties.DemoProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @time: 2019/08/13
* @author: Vgbh
*/
//开启配置
// 开启使用映射实体
public class VgbhfiveAutoConfigure {
public VgbhfiveClient vgbhfiveClient(DemoProperties demoProperties) {
//System.out.print(" ------- VgbhfiveClient Created! -------");
return new VgbhfiveClient(demoProperties);
}
}服务类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package cn.vgbhfive.client;
import cn.vgbhfive.properties.DemoProperties;
/**
* @time: 2019/08/13
* @author: Vgbh
*/
public class VgbhfiveClient {
private DemoProperties demoProperties; // 配置参数
public VgbhfiveClient () {
}
public VgbhfiveClient(DemoProperties p) {
this.demoProperties = p;
}
public String getName() {
return demoProperties.getName();
}
}开启服务类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package cn.vgbhfive.annotation;
import cn.vgbhfive.config.VgbhfiveAutoConfigure;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @time: 2019/08/14
* @author: Vgbh
*/
public EnableVgbhfiveCient {
}扫描自动配置类文件
1
2org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.vgbhfive.config.VgbhfiveAutoConfigure配置参数提醒
1
2
3
4
5
6
7
8{
"properties":[
{
"name":"spring.vgbhfive.user",
"defaultValue":"vgbhfive"
}
]
}
备注
Spring Boot 官方建议个人开发使用xxx-spring-boot-starter 这种格式作为包名。
存放地址
https://github.com/vgbhfive/SpringBootDemo -> springbootstarterdemo
使用自定义Starter 项目
pom.xml :
1 |
|
配置文件application.properties
1
spring.vgbhfive.name=vgbh
开启服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package cn.vgbhfive.springbootstarterdemotest;
import cn.vgbhfive.annotation.EnableVgbhfiveCient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SpringbootstarterdemotestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootstarterdemotestApplication.class, args);
}
}对外预留接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package cn.vgbhfive.springbootstarterdemotest;
import cn.vgbhfive.client.VgbhfiveClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @time: 2019/08/14
* @author: Vgbh
*/
public class DemoController {
private VgbhfiveClient vgbhfiveClient;
public String test() {
String name = vgbhfiveClient.getName();
if (null == name) {
return "Hello World!";
}
return name;
}
}输出结果
实战项目
https://github.com/vgbhfive/vid-spring-boot-starter
参考资料
个人备注
此内容均为作者学习所做笔记!
转做其他用途必经作者同意,假冒转载必究!