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

如何实现自己的spring boot starter(一)

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

如何实现自己的spring boot starter(一)

目录

背景:

客户端 my-spring-boot-starter-client:

POM

HelloWorldProperties

 HelloWorldAutoConfiguration

HelloWorld

meta-INF:

服务端:my-spring-boot-starter-consumer

POM:

 application.properties

TestController

测试:

客户端打jar包

服务端引用坐标

启动服务端,请求 localhost:8080/print


背景:

一般公司中,都会有自己内部的jar包提供出来供开发人员引用,我们接触了SpringBoot发现里面有很多的Starter供我们引用,那我们能不能模仿者SpringBoot来自定义我们自己的Starter类型的jar呢

这边有两个工程,一个是自己提供给别人的jar包(我叫它为客户端), 一个是别人使用我们的jar(消费者)

客户端 my-spring-boot-starter-client:

用idea新建一个maven项目 my-spring-boot-starter-client,我的代码结构是下面这样的:

POM


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    

    com.my.boot.starter
    my-spring-boot-starter-client
    1.0-SNAPSHOT

    
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
    

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

HelloWorldProperties
package com.my.boot.starter.autoconfigure;

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

@ConfigurationProperties(prefix = "com.my.boot")
public class HelloWorldProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 HelloWorldAutoConfiguration
package com.my.boot.starter.autoconfigure;

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

@Configuration
@EnableConfigurationProperties({HelloWorldProperties.class})
public class HelloWorldAutoConfiguration {

    private HelloWorldProperties properties;

    @Autowired
    public HelloWorldAutoConfiguration(HelloWorldProperties properties) {
        this.properties = properties;
    }

    @Bean
    public HelloWorld helloWorld() {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setProperties(properties);
        return helloWorld;
    }
}

HelloWorld
package com.my.boot.starter.autoconfigure;

public class HelloWorld {

    private HelloWorldProperties properties;

    public void setProperties(HelloWorldProperties properties) {
        this.properties = properties;
    }

    public void print() {
        System.out.println("name:" + this.properties.getName());
    }
}

meta-INF:

resources 下面新建 meta-INF 包,新增 spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
 com.my.boot.starter.autoconfigure.HelloWorldAutoConfiguration

服务端:my-spring-boot-starter-consumer

用idea新建一个SpringBoot项目 my-spring-boot-starter-consumer,我的代码结构是下面这样的:

POM:


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.my.boot.consumer
    my-spring-boot-starter-consumer
    0.0.1-SNAPSHOT
    my-spring-boot-starter-consumer
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.my.boot.starter
            my-spring-boot-starter-client
            1.0-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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


 application.properties
com.my.boot.name=maple

TestController
package com.my.boot.consumer.controller;

import com.my.boot.starter.autoconfigure.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    private HelloWorld helloWorld;

    @Autowired
    public TestController(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }

    @GetMapping("/print")
    private void test() {
        helloWorld.print();
    }
}

测试:

客户端打jar包

 my-spring-boot-starter-client

maven clean install 

服务端引用坐标
        
            com.my.boot.starter
            my-spring-boot-starter-client
            1.0-SNAPSHOT
        

启动服务端,请求 localhost:8080/print

 

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

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

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