最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBoot如何自定義自己的Starter組件

 更新時(shí)間:2024年03月11日 11:29:53   作者:HBLOG  
這篇文章主要為大家詳細(xì)介紹了在SpringBoot中如何自定義自己的Starter組件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、為什么要自定義starter

在我們的日常開(kāi)發(fā)工作中,經(jīng)常會(huì)有一些獨(dú)立于業(yè)務(wù)之外的配置模塊,我們經(jīng)常將其放到一個(gè)特定的 包下,然后如果另一個(gè)工程需要復(fù)用這塊功能的時(shí)候,需要將代碼硬拷貝到另一個(gè)工程,重新集成一 遍,麻煩至極。如果我們將這些可獨(dú)立于業(yè)務(wù)代碼之外的功能配置模塊封裝成一個(gè)個(gè)starter,復(fù)用的時(shí) 候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動(dòng)裝配,簡(jiǎn)直不要太爽。

二、starter的實(shí)現(xiàn)

雖然不同的starter實(shí)現(xiàn)起來(lái)各有差異,但是他們基本上都會(huì)使用到兩個(gè)相同的內(nèi)容:ConfigurationPropertiesAutoConfiguration。因?yàn)?code>Spring Boot堅(jiān)信“約定大于配置”這一理念,所以我們使用ConfigurationProperties來(lái)保存我們的配置,并且這些配置都可以有一個(gè)默認(rèn)值,即在我們沒(méi)有主動(dòng)覆寫(xiě)原始配置的情況下,默認(rèn)值就會(huì)生效,這在很多情況下是非常有用的。除此之外,starterConfigurationProperties還使得所有的配置屬性被聚集到一個(gè)文件中(一般在resources目錄下的application.properties),這樣我們就告別了Spring項(xiàng)目中XML地獄。

三、命名規(guī)范

如果你快有孩子了,出生前你比較急的一定是起個(gè)名字。孩子的姓名標(biāo)識(shí)著你和你愛(ài)人的血統(tǒng),一定不會(huì)起隔壁老王的姓氏,肯定會(huì)招來(lái)異樣的眼光。在maven中,groupId代表著姓氏,artifactId代表著名字。Spring Boot也是有一個(gè)命名的建議的。所以名字是不能夠隨隨便便取得,可以按照官方的建議來(lái)取。

What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-, whereis a particular type of application. This naming structure is intended to help when you need to find a starter. The Maven integration in many IDEs lets you search dependencies by name. For example, with the appropriate Eclipse or STS plugin installed, you can press ctrl-space in the POM editor and type “spring-boot-starter” for a complete list. As explained in the “Creating Your Own Starter” section, third party starters should not start with spring-boot, as it is reserved for official Spring Boot artifacts. Rather, a third-party starter typically starts with the name of the project. For example, a third-party starter project called thirdpartyproject would typically be named thirdpartyproject-spring-boot-starter.

大概意思:官方的 starter 的命名格式為 spring-boot-starter-{xxxx} 比如spring-boot-starter-activemq,第三方我們自己的命名格式為 {xxxx}-spring-boot-starter。比如mybatis-spring-boot-starter。如果我們忽略這種約定,是不是會(huì)顯得我們寫(xiě)的東西不夠“專業(yè)“。

四、代碼工程

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>xxx-spring-boot-starter</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies> 

屬性文件

com.person.age=23
com.person.name=Lynch
com.person.sex=F

自動(dòng)配置類(lèi)

package com.et.config;

import com.et.service.PersonService;
import com.et.starter.PersonProperties;
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 
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "com.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {

    @Autowired
    private PersonProperties properties;

    // if spring container do not config bean,auto config PersonService
    @Bean
    @ConditionalOnMissingBean(PersonService.class)  
    public PersonService personService(){
        PersonService personService = new PersonService(properties);
        return personService;
    }
}

service類(lèi)

package com.et.service;

import com.et.starter.PersonProperties;

public class PersonService {
    private PersonProperties properties;

    public PersonService() {
    }

    public PersonService(PersonProperties properties) {
        this.properties = properties;
    }

    public void sayHello() {
        String message = String.format("hi,my name: %s, today,I'am %s , gender: %s",
                properties.getName(), properties.getAge(), properties.getSex());
        System.out.println(message);
    }
}

PersonProperties

package com.et.starter;

import java.io.Serializable;

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

@SuppressWarnings("serial")
@ConfigurationProperties(prefix = "com.person")
public class PersonProperties implements Serializable {
    private String name;
    private int age;
    private String sex = "M";

    public PersonProperties() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}

spring.factories文件

/META-INF/spring.factories文件放在/src/main/resources目錄下 注意:META-INF是自己手動(dòng)創(chuàng)建的目錄,spring.factories也是自己手動(dòng)創(chuàng)建的文件,在該文件中配置自己的自動(dòng)配置類(lèi)。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.et.config.PersonServiceAutoConfiguration

項(xiàng)目打包

 mvn clean install

代碼倉(cāng)庫(kù)

github.com/Harries/springboot-demo

五、測(cè)試

在另外一個(gè)項(xiàng)目中添加starter的依賴

<dependency>
    <groupId>com.et</groupId>
    <artifactId>xxx-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

單元測(cè)試類(lèi)

package com.et.starter;

import com.et.service.PersonService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonServiceTest {
    @Autowired
    private PersonService personService;

    @Test
    public void testHelloWorld() {
        personService.sayHello();
    }
}

運(yùn)行測(cè)試類(lèi)

2024-03-11 10:35:18.374 INFO 10960 --- [ main] com.et.starter.PersonServiceTest : Starting PersonServiceTest on BJDPLHHUAPC with PID 10960 (started by Dell in D:\IdeaProjects\ETFramework\xxx-spring-boot-starter-test)
2024-03-11 10:35:18.376 INFO 10960 --- [ main] com.et.starter.PersonServiceTest : No active profile set, falling back to default profiles: default
2024-03-11 10:35:19.387 INFO 10960 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2024-03-11 10:35:19.657 INFO 10960 --- [ main] com.et.starter.PersonServiceTest : Started PersonServiceTest in 1.507 seconds (JVM running for 2.188)
hi,my name: Lynch, today,I'am 23 , gender: F
2024-03-11 10:35:19.827 INFO 10960 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'

以上就是詳解SpringBoot如何自定義自己的Starter組件的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot自定義Starter組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

中宁县| 灵寿县| 大名县| 黄陵县| 扶绥县| 平南县| 安阳市| 平和县| 华坪县| 将乐县| 江城| 渑池县| 柳林县| 鄄城县| 樟树市| 新源县| 乌拉特后旗| 贵州省| 海丰县| 平湖市| 贺兰县| 平原县| 上思县| 铜梁县| 吴旗县| 曲阳县| 枞阳县| 宜城市| 新河县| 太仓市| 海伦市| 宁强县| 定兴县| 迁西县| 封丘县| 博乐市| 汶上县| 辉县市| 亳州市| 二连浩特市| 湖口县|