栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于springboot2.5.5自建启动器starter制品库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于springboot2.5.5自建启动器starter制品库

【README】

本文po出了自建springboot 启动器步骤;


【1】新建2个starter相关组件

根据  mybatis-spring-boot-starter,我们看到 自建starter需要两个组件,分别是 xxx-spring-boot-starter, xxx-spring-boot-starter-autoconfigure ;

其中,xxx-spring-boot-starter负责引入 xxx-spring-boot-starter-autoconfigure 依赖; xxx-spring-boot-starter-autoconfigure 负责定义相关配置;

step1,新建空项目 ;

项目名称为: springbt-08-starter-diy2;

step2,在空项目上新建2个组件,分别为

lisi-spring-boot-starter 和 lisi-spring-boot-starter-autoconfigure;

其中, lisi-spring-boot-starter为 单纯的maven项目;

 lisi-spring-boot-starter-autoconfigure 通过 spring Initializer 工具创建;

step3, lisi-spring-boot-starter 引入 lisi-spring-boot-starter-autoconfigure 依赖;

lisi-spring-boot-starter组件的pom.xml 如下:



    4.0.0

    com.cmc.starter
    lisi-spring-boot-starter
    1.0-SNAPSHOT

    
        
        
            com.cmc.starter
            lisi-spring-boot-starter-autoconfigure
            0.0.1-SNAPSHOT
        
    

并把测试目标目录删除, lisi-spring-boot-starter目录结构如下:

 step4, lisi-spring-boot-starter-autoconfigure 组件的pom.xml 如下:

(删除多余的插件, 测试库依赖)



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.cmc.starter
    lisi-spring-boot-starter-autoconfigure
    0.0.1-SNAPSHOT
    lisi-spring-boot-starter-autoconfigure
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        
    



【2】开发 starter-autoconfigure 启动器配置组件

step1, 新建 HelloService,HelloProperties, HelloServiceAutoConfiguration ;

package com.cmc.starter.lisi;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "cmc.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
package com.cmc.starter.lisi;

public class HelloService {

    HelloProperties helloProperties;

    public String sayHelloCmc(String name) {
        return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}
package com.cmc.starter.lisi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(HelloProperties.class) // 使属性文件生效
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

step2,在资源类路径下新建 meta-INF/spring.factories 属性文件;添加 自动配置类全限定类名;

# spring.factories

# Auto Config
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.cmc.starter.lisi.HelloServiceAutoConfiguration

参考的是 spring-boot-autoconfigure.jar 中的 meta-INF/spring.factories的内容;

目录结构如下:

step3,先把 lisi-spring-boot-starter-autoconfigure 制品库安装到本地仓库

step4,再把 lisi-spring-boot-starter 制品库安装到本地仓库;


【3】自建starter使用

step1, 新建项目,只需要引入 lisi-spring-boot-starter依赖  即可, 因为 lisi-spring-boot-starter  引入了 lisi-spring-boot-starter-autoconfigure,传递依赖;

pom.xml 如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.cmc
    springboot-08-starter-test2
    0.0.1-SNAPSHOT
    springboot-08-starter-test2
    Demo project for Spring Boot
    
        1.8
    
    
        
        
            com.cmc.starter
            lisi-spring-boot-starter
            1.0-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


step2, 新建测试项目;编写 Controller,引入 HelloService

import com.cmc.starter.lisi.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.sayHelloCmc("zhangsan");
    }
}

step3,HelloServiceAutoConfiguration有两个属性前缀后缀,需要配置;在 application.properties 中配置如下:

cmc.hello.prefix=chengdu
cmc.hello.suffix=028

step4,启动springboot项目,并访问

localhost:8080/hello ;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/332100.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号