SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用,本文简单介绍封装一个简单的服务信息监控starter包,用于其它工程引入后配置yml文件一些基本信息,启动后即可访问url获取该工程服务信息。
二、如何自定义starter1. 引入starter需要的pom
org.springframework.boot
spring-boot-autoconfigure
2.1.3.RELEASE
org.springframework.boot
spring-boot-configuration-processor
2.1.3.RELEASE
org.projectlombok
lombok
1.18.20
provided
2.创建 ZmProperties
package com.zmstrater.zmspringbootstrater.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "com.zm")
public class ZmProperties {
private String prot;
private String project;
private String id;
}
3.创建配置类 ActuatorAutoConfiguration
package com.zmstrater.zmspringbootstrater.config;
import com.zmstrater.zmspringbootstrater.entity.ZmProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(ZmProperties.class)
public class ActuatorAutoConfiguration {
}
4.创建访问类 StraterEndPoint
package com.zmstrater.zmspringbootstrater.web;
import com.zmstrater.zmspringbootstrater.entity.ZmProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class StraterEndPoint {
@Autowired
private ZmProperties zmProperties;
@GetMapping(value = "methodStarter")
public Map methodStarter(){
Map map = new HashMap<>();
map.put("Properties",zmProperties.getProject());
map.put("Prot",zmProperties.getProt());
map.put("Id",zmProperties.getId());
return map;
}
}
5.配置 spring.factories,在resources/meta-INF下创建spring.factories
在spring.factories里装配需要加载的类:
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.zmstrater.zmspringbootstrater.config.ActuatorAutoConfiguration, com.zmstrater.zmspringbootstrater.web.StraterEndPoint
6.打包
三、其它工程引入该strater
1.把jar拷贝到本地文件夹中
2.工程引入刚才打包的strater
在图中第二步输入本地打包命令
install:install-file -Dfile=D:TESTZMJARzm-springboot-strater-0.0.2-SNAPSHOT.jar -DgroupId=com.zmstrater -DartifactId=zm-springboot-strater -Dversion=0.0.2 -Dpackaging=jar
3.配置工程yml文件
com:
zm:
id: 34ae4d4r
project: ZMceshi
prot: 8090
4.启动本地访问:http://127.0.0.1:8083/methodStarter
访问成功



