栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

手写spring boot starter

手写spring boot starter


MyProperties

package com.jiading;

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

@ConfigurationProperties(prefix = "spring.jiading")
public class MyProperties {
    private String host;

    private int port;
    //省略 get set 方法

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public int getPort()
    {
        return port;
    }

    public void setPort(int port)
    {
        this.port = port;
    }
}


MyService

package com.jiading;

public class MyService {

    private String host;

    private int port;

    public MyService(){

    }
    public MyService(MyProperties myProperties){
        this.host = myProperties.getHost();
        this.port = myProperties.getPort();
    }

    public void print(){
        System.out.println((this.host + ":" + this.port));
    }
}

MyAutoConfigure

package com.jiading;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;

@Configuration
@ConditionalOnClass(MyService.class)
@EnableConfigurationProperties(MyProperties.class)
@ConditionalOnProperty(prefix = "spring.jiading",value = "enabled", havingValue = "true")
public class MyAutoConfigure {

    @Autowired
    private MyProperties myProperties;

    @Bean
    @ConditionalOnMissingBean(MyService.class)
    public MyService myService(){
        return new MyService(myProperties);
    }
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  com.jiading.MyAutoConfigure

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
    
    com.jiading
    lemongo-spring-boot-starter
    0.0.1
    lemongo-spring-boot-starter
    lemongo-spring-boot-starter
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-configuration-processor
        
        
            org.springframework.boot
            spring-boot-autoconfigure
        
    
//不要下面的,就算这么设置了,不要main方法了,但是最后打出来的包会多一层BOOT-INF文件夹














然后在另一个项目中引入即可


            com.jiading
            lemongo-spring-boot-starter
            0.0.1
        
在这里插入代码片

注意application.yml里要有enabled属性

spring:
  jiading:
    enabled: true
    host: 123123
    port: 8888
@RestController
public class TestController
{
    @Resource
    private MyService myService;

    @GetMapping("/test")
    public int test(){
        myService.print();
        return 1;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/707949.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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