亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot中的自定義Starter解讀

 更新時(shí)間:2023年12月27日 11:05:04   作者:學(xué)哥斌  
這篇文章主要介紹了SpringBoot中的自定義Starter解讀,啟動(dòng)器模塊其實(shí)是一個(gè)空的jar文件,里面沒有什么類、接口,僅僅是提供輔助性依賴管理,這些依賴可能用于自動(dòng)裝配或者其他類庫,需要的朋友可以參考下

1、說明

啟動(dòng)器模塊其實(shí)是一個(gè)空的jar文件,里面沒有什么類、接口,僅僅是提供輔助性依賴管理,這些依賴可能用于自動(dòng)裝配或者其他類庫;

在這里插入圖片描述

官方的命名方式是:spring-boot-starter-xxx,例如:spring-boot-starter-web。

官方提倡的自定義starter命名方式要和他們區(qū)分開,官方的xxx是在最后面,提議第三方自定義的starter的xxx放在最前面

即:xxx-spring-boot-starter,例如:mybatis-spring-boot-starter。

2、自定義starter

2.1 工程構(gòu)建

IDEA新建普通java空項(xiàng)目:

在這里插入圖片描述

在這里插入圖片描述

1、新建一個(gè)普通maven模塊:

在這里插入圖片描述

在這里插入圖片描述

2、新建一個(gè)SpringBoot模塊:

在這里插入圖片描述

構(gòu)建好了后,結(jié)構(gòu)如下:

在這里插入圖片描述

2.2 starter模塊導(dǎo)入autoconfigure模塊的依賴

在這里插入圖片描述

2.3 清理autoconfigure模塊

新建的空SpringBoot模塊刪除其他無關(guān)的文件和文件夾,同時(shí)pom.xml只留下sarter:

在這里插入圖片描述

2.4 編輯autoconfigure模塊

autoconfigure模塊總共要寫三個(gè)文件:service、properties和antoconfiguration。這三個(gè)分別的作用是:提供服務(wù)、配置類(定義服務(wù)相關(guān)的屬性配置)和自動(dòng)配置功能。

2.4.1 編寫服務(wù)

package cn.klb.boot.autoconfigure;
/**
 * @Author: Konglibin
 * @Description:
 * @Date: Create in 2020/4/24 11:21
 * @Modified By:
 */
public class CalculatorService {
    CalculatorProperties properties;
    public CalculatorProperties getProperties() {
        return properties;
    }
    public void setProperties(CalculatorProperties properties) {
        this.properties = properties;
    }
    public String getAreaOfCircle(Double radius) {
        Double area = properties.getPi() * radius * radius;
        return Double.toString(area);
    }
}

2.4.2 編寫配置類

package cn.klb.boot.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * @Author: Konglibin
 * @Description:
 * @Date: Create in 2020/4/24 11:21
 * @Modified By:
 */
@ConfigurationProperties(prefix = "calculator.constant")
public class CalculatorProperties {
    private Double pi;
    private Double e;
    public Double getPi() {
        return pi;
    }
    public void setPi(Double pi) {
        this.pi = pi;
    }
    public Double getE() {
        return e;
    }
    public void setE(Double e) {
        this.e = e;
    }
}

2.4.3 編寫自動(dòng)配置并注入bean

package cn.klb.boot.autoconfigure;
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;
/**
 * @Author: Konglibin
 * @Description:
 * @Date: Create in 2020/4/24 11:22
 * @Modified By:
 */
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(CalculatorProperties.class)
public class CalculatorServiceAutoConfiguration {
    @Autowired
    CalculatorProperties properties;
    @Bean
    public CalculatorService getCalculatorService(){
        CalculatorService service = new CalculatorService();
        service.setProperties(properties);
        return service;
    }
}

2.4.4 編寫自己的spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.klb.boot.autoconfigure.CalculatorServiceAutoConfiguration

2.5 統(tǒng)一打包安裝

大部分工作都是在編輯autoconfigure模塊,編輯好之后工程如下:

在這里插入圖片描述

執(zhí)行maven的install命令(注意先安裝autoconfigure再安裝starter,否則會(huì)找不到包)。

3、測(cè)試

新建一個(gè)SpringBoot工程,并導(dǎo)入我們自己寫的啟動(dòng)器:

在這里插入圖片描述

編寫一個(gè)控制器:

package cn.klb.springboot_helloworld.controller;
import cn.klb.boot.autoconfigure.CalculatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Author: Konglibin
 * @Description:
 * @Date: Create in 2020/4/24 11:44
 * @Modified By:
 */
@RestController
public class CalculatorController {
    @Autowired
    CalculatorService service;
    @RequestMapping("/cal")
    public String calculateArea(){
        return service.getAreaOfCircle(2.0);
    }
}

application.yaml配置文件配置屬性:

calculator:
  constant:
    pi: 3.14
    e: 2.0

啟動(dòng)后瀏覽器輸入自己定義的地址,得到結(jié)果如下:

在這里插入圖片描述

結(jié)果成功!

到此這篇關(guān)于SpringBoot中的自定義Starter解讀的文章就介紹到這了,更多相關(guān)自定義Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論