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

如何實(shí)現(xiàn)自定義SpringBoot的Starter組件

 更新時(shí)間:2023年02月08日 10:13:08   作者:令狐前生  
這篇文章主要介紹了實(shí)現(xiàn)自定義SpringBoot的Starter組件的示例代碼,想要自定義starter組件,首先要了解springboot是如何加載starter的,也就是springboot的自動(dòng)裝配機(jī)制原理,本文結(jié)合示例代碼詳細(xì)講解,需要的朋友可以參考下

一、前言

想要自定義starter組件,首先要了解springboot是如何加載starter的,也就是springboot的自動(dòng)裝配機(jī)制原理。

1.1、starter加載原理

springboot通過一個(gè)@SpringBootApplication注解啟動(dòng)項(xiàng)目,springboot在項(xiàng)目啟動(dòng)的時(shí)候,會(huì)將項(xiàng)目中所有聲明為Bean對象(注解、xml)的實(shí)例信息全部加載到ioc容器當(dāng)中。 除此之外也會(huì)將所有依賴到的starter里的bean信息加載到ioc容器中,從而做到所謂的零配置,開箱即用。

1.1.1、加載starter

首先通過通過注解@SpringBootApplication找到@EnableAutoConfiguration注解進(jìn)行加載starter。

再通過注解@EnableAutoConfiguration下注解@import找到AutoConfigurationImportSelector類加載器實(shí)現(xiàn)。

這個(gè)AutoConfigurationImportSelector類會(huì)去其引用的依賴jar包下,找到一個(gè)”spring.factories”文件,一般spring.factories文件里都會(huì)聲明該依賴所提供的核心功能bean配置信息。文件一般在依賴jar包的META-INF文件夾下面。

以spring-boot版本2.7.7為例,加載spring.factories的代碼在:

AutoConfigurationImportSelector.java->selectImports(AnnotationMetadata annotationMetadata)->getAutoConfigurationEntry(annotationMetadata)->getCandidateConfigurations(annotationMetadata, attributes)->SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader())->loadSpringFactories(classLoaderToUse):

private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
        Map<String, List<String>> result = (Map)cache.get(classLoader);
        if (result != null) {
            return result;
        } else {
            HashMap result = new HashMap();
 
            try {
                Enumeration urls = classLoader.getResources("META-INF/spring.factories");
 
                while(urls.hasMoreElements()) {
                    URL url = (URL)urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    Iterator var6 = properties.entrySet().iterator();
 
                    while(var6.hasNext()) {
                        Entry<?, ?> entry = (Entry)var6.next();
                        String factoryTypeName = ((String)entry.getKey()).trim();
                        String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                        String[] var10 = factoryImplementationNames;
                        int var11 = factoryImplementationNames.length;
 
                        for(int var12 = 0; var12 < var11; ++var12) {
                            String factoryImplementationName = var10[var12];
                            ((List)result.computeIfAbsent(factoryTypeName, (key) -> {
                                return new ArrayList();
                            })).add(factoryImplementationName.trim());
                        }
                    }
                }
 
                result.replaceAll((factoryType, implementations) -> {
                    return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
                });
                cache.put(classLoader, result);
                return result;
            } catch (IOException var14) {
                throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14);
            }
        }
    }

舉例如:spring-boot-autoconfig的spring.factories.

二、自定義starter

上面了解了springboot加載starter原理,其實(shí)就是加載依賴jar包下的spring.factories文件。所以我們要自定義starter,就需要在項(xiàng)目中建立一個(gè)META-INF的文件夾,然后在該文件夾下面建一個(gè)spring.factories文件,文件里將你需要提供出去的bean實(shí)例信息配置好就行。

2.1、代碼

2.1.1、新建springboot項(xiàng)目。

簡單演示所以需求配置任務(wù)依賴。如springboot構(gòu)建很慢,或者打包的時(shí)候下載依賴很慢,可在pom文件中添加如下倉庫配置,可以加快構(gòu)建速度。

 <repositories>
        <repository>
            <id>alimaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
 
    <pluginRepositories>
        <pluginRepository>
            <id>alimaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </pluginRepository>
    </pluginRepositories>

注意:spring官方規(guī)定自定義組件的命名:

SpringBoot官方命名方式
格式:spring-boot-starter-{模塊名}
舉例:spring-boot-starter-web
自定義命名方式
格式:{模塊名}-spring-boot-starter
舉例:mystarter-spring-boot-starter

2.1.2、項(xiàng)目構(gòu)建完成后,在resources文件夾下面新建META-INF文件夾,并新建spring.factories文件。

2.1.3、因?yàn)槲覀兪亲鳛椴寮硎褂?,所以我們不需要啟?dòng)類,刪除啟動(dòng)類。并新建幾個(gè)類:

一個(gè)接口AnimalService:

package com.example.demospringbootstarter.service;
 
/**
 * @Project: demo-spring-boot-starter
 * @Description:
 * @Author: chengjiangbo
 * @Date: 2023/2/7  15:12
 */
public interface AnimalService {
 
    String say();
}

兩個(gè)接口實(shí)現(xiàn)類CatService和DogService:

package com.example.demospringbootstarter.service;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;
 
/**
 * @Project: demo-spring-boot-starter
 * @Description:
 * @Author: chengjiangbo
 * @Date: 2023/2/7  14:49
 */
@Service
public class CatService implements AnimalService{
 
    public static String name = "cat";
 
    @Override
    public String say() {
        return "喵喵";
    }
}
package com.example.demospringbootstarter.service;
 
import org.springframework.stereotype.Service;
 
/**
 * @Project: demo-spring-boot-starter
 * @Description:
 * @Author: chengjiangbo
 * @Date: 2023/2/7  14:49
 */
@Service
public class DogService implements AnimalService{
 
    public static String name = "dog";
 
    @Override
    public String say() {
        return "汪汪";
    }
}

再建一個(gè)配置AnimalProperties類,方便注入屬性值:

package com.example.demospringbootstarter.config;
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
 
/**
 * @Project: demo-spring-boot-starter
 * @Description:
 * @Author: chengjiangbo
 * @Date: 2023/2/7  15:37
 */
@Data
@ConfigurationProperties(prefix = "animal")
public class AnimalProperties {
 
    private String name;
}

最后新建一個(gè)核心自動(dòng)裝備配置類:

package com.example.demospringbootstarter.config;
 
import com.example.demospringbootstarter.service.AnimalService;
import com.example.demospringbootstarter.service.CatService;
import com.example.demospringbootstarter.service.DogService;
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;
 
/**
 * @Project: demo-spring-boot-starter
 * @Description:
 * @Author: chengjiangbo
 * @Date: 2023/2/7  14:48
 */
@Configuration
@EnableConfigurationProperties(AnimalProperties.class)
public class AnimalAutoConfig {
 
    @Autowired
    private AnimalProperties animalProperties;
 
    @Bean
    public AnimalService demoService(){
        switch (animalProperties.getName()){
            case "cat":
                return new CatService();
            case "dog":
                return new DogService();
            default:
                return null;
        }
    }
}

META-INF/spring.factories的內(nèi)容為:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.demospringbootstarter.config.AnimalAutoConfig

以上步驟都好后,使用maven命令打包:

mvn clean install -Dmaven.test.skip=true

或者使用idea的LIfecycle點(diǎn)擊對應(yīng)操作(注意不是plugin下的命令操作)。

pom.xml內(nèi)容為:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>0.0.4-SNAPSHOT</version>
    <name>demo-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
 
    </dependencies>
 
    <repositories>
        <repository>
            <id>alimaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
 
    <pluginRepositories>
        <pluginRepository>
            <id>alimaven</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </pluginRepository>
    </pluginRepositories>
 
</project>

三、組件集成依賴測試

3.1、新啟另一個(gè)項(xiàng)目中,引入剛剛打包的pom依賴

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo-spring-boot-starter</artifactId>
            <version>0.0.4-SNAPSHOT</version>
        </dependency>

3.2、新建一個(gè)controller,里面注入上面提供的AnimalService類并調(diào)用其方法

package com.cjb.mavendemo.controllers;
 
import com.example.demospringbootstarter.service.AnimalService;
import com.example.inputoutputlogspringbootstarter.config.PrintResponseTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @Project: maven-demo
 * @Description:
 * @Author: chengjiangbo
 * @Date: 2023/2/7  10:26
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {
 
    @Autowired
    private AnimalService animalService;
 
    @PrintResponseTime
    @GetMapping("/call")
    public String call(){
        return animalService.say();
    }
}

3.3、application.properties內(nèi)容配置參數(shù)"animal.name"值

3.4、最后通過項(xiàng)目啟動(dòng)類啟動(dòng)項(xiàng)目(項(xiàng)目啟動(dòng)類就一個(gè)@SpringBootApplicaiton注解)

package com.cjb.mavendemo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class MavenDemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MavenDemoApplication.class, args);
    }
 
}

3.5、接口測試

調(diào)用http接口測試:

修改"animal.name"值為"cat",再次調(diào)用http接口訪問:

四、源碼地址,參考資料

  1. 組件代碼:https://download.csdn.net/download/u010132847/87426046

    1. 集成自定義組件代碼:https://download.csdn.net/download/u010132847/87426048

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

相關(guān)文章

  • 有關(guān)ThreadLocal的面試題你真的懂了嗎

    有關(guān)ThreadLocal的面試題你真的懂了嗎

    這篇文章主要介紹了面試題ThreadLocal,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-06-06
  • Java經(jīng)典設(shè)計(jì)模式之責(zé)任鏈模式原理與用法詳解

    Java經(jīng)典設(shè)計(jì)模式之責(zé)任鏈模式原理與用法詳解

    這篇文章主要介紹了Java經(jīng)典設(shè)計(jì)模式之責(zé)任鏈模式,簡單說明了責(zé)任鏈模式的概念、原理,并結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)責(zé)任鏈模式的具體用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08
  • java實(shí)現(xiàn)在一張大圖片上添加小圖及文字

    java實(shí)現(xiàn)在一張大圖片上添加小圖及文字

    這篇文章主要介紹了java實(shí)現(xiàn)在一張大圖上添加小圖及文字,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Springboot集成阿里云OSS上傳文件系統(tǒng)教程

    Springboot集成阿里云OSS上傳文件系統(tǒng)教程

    這篇文章主要介紹了Springboot集成阿里云OSS上傳文件系統(tǒng)教程,通過詳細(xì)的圖文展示,代碼步驟的展示和文件配置信息,希望對你有所幫助
    2021-06-06
  • nacos單機(jī)本地配置文件存儲(chǔ)位置方式

    nacos單機(jī)本地配置文件存儲(chǔ)位置方式

    這篇文章主要介紹了nacos單機(jī)本地配置文件存儲(chǔ)位置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • logback使用MDCFilter日志過濾源碼解讀

    logback使用MDCFilter日志過濾源碼解讀

    這篇文章主要介紹了logback使用MDCFilter日志過濾源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 詳解springboot如何更新json串里面的內(nèi)容

    詳解springboot如何更新json串里面的內(nèi)容

    這篇文章主要為大家介紹了springboot 如何更新json串里面的內(nèi)容,文中有詳細(xì)的解決方案供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-10-10
  • Java中SpringBoot自定義Starter詳解

    Java中SpringBoot自定義Starter詳解

    這篇文章主要介紹了Java中SpringBoot自定義Starter詳解,Starter是Spring Boot中的一個(gè)非常重要的概念,Starter相當(dāng)于模塊,它能將模塊所需的依賴整合起來并對模塊內(nèi)的Bean根據(jù)環(huán)境進(jìn)行自動(dòng)配置,需要的朋友可以參考下
    2023-07-07
  • spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開發(fā)環(huán)境搭建圖文教程

    spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開發(fā)環(huán)境搭

    這篇文章主要介紹了spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11+mysql5.5.25開發(fā)環(huán)境搭建圖文教程,需要的朋友可以參考下
    2016-06-06
  • MybatisX中xml映射文件中命名空間爆紅的解決

    MybatisX中xml映射文件中命名空間爆紅的解決

    本文主要介紹了MybatisX中xml映射文件中命名空間爆紅的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06

最新評論