自制作spring boot starter

spring boot starter

代碼

代碼github鏈接 common
代碼github鏈接 chase

自制starter

maven項目依賴,只需依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.4.0</version>
    <scope>compile</scope>
</dependency>

配置外部化

package common;


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

/**
 * @author chase
 */
@ConfigurationProperties(prefix = "common.service")
public class CommonProperties {

    private String username;
    private String password;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

服務(wù)類編寫

package common;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author chase
 */
@Service
public class CommonService {

    @Autowired
    private CommonProperties commonProperties;
    
    /**
     * 外部調(diào)用方法
     * @return
     */
    public String sayHello() {
        return commonProperties.getUsername() ;
    }
}

配置類

package common;

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;

/**
 * @author chase
 */
@Configuration
@ConditionalOnClass(CommonService.class)
@ConditionalOnProperty(prefix = "common.service",value = "enable",matchIfMissing = true)
@EnableConfigurationProperties(CommonProperties.class)
public class CommonServiceAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public CommonService greeter() {
        return new CommonService();
    }
}

創(chuàng)建自動裝配配置文件

創(chuàng)建在resources/META-INF目錄下創(chuàng)建spring.factories文件,文件內(nèi)容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
common.CommonServiceAutoConfiguration

把該maven項目 install到本地倉庫,有私服的可以上傳到私服,至此一個簡單的spring boot starter已制作完成

在spring boot項目中使用自制作的starter

pom引入自制的starter

<dependency>
   <groupId>com.chase</groupId>
   <artifactId>common-spring-boot-starter</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

配置starter的配置文件

在application.properties文件中,配置

common.service.username=admin
common.service.password=admin23

注入starter中的服務(wù),并調(diào)用

package com.league.chase.web;


import common.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CommonController {


    @Autowired
    private CommonService commonService;

    @GetMapping("/sayHello")
    public String sayHello() {
        return commonService.sayHello();
    }
}

測試

http://localhost:8080/sayHello 輸出 admin

一個簡單的starter已經(jīng)完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容