SpringBoot 自定义starter包,(封装一个其它工程可以引入配置的简单服务信息监控包)

一、简介

SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用,本文简单介绍封装一个简单的服务信息监控starter包,用于其它工程引入后配置yml文件一些基本信息,启动后即可访问url获取该工程服务信息。

二、如何自定义starter

1. 引入starter需要的pom

        <!--strater-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

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<String,Object> methodStarter(){
        Map<String,Object> 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:\TESTZM\JAR\zm-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

访问成功

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值