手写spring boot starter

手写spring boot starter,第1张

手写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;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5706504.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存