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

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

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

一、為什么要自定義starter

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

二、starter的實現

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

三、命名規(guī)范

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

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。如果我們忽略這種約定,是不是會顯得我們寫的東西不夠“專業(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

自動配置類

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類

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是自己手動創(chuàng)建的目錄,spring.factories也是自己手動創(chuàng)建的文件,在該文件中配置自己的自動配置類。

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

項目打包

 mvn clean install

代碼倉庫

github.com/Harries/springboot-demo

五、測試

在另外一個項目中添加starter的依賴

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

單元測試類

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();
    }
}

運行測試類

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組件的詳細內容,更多關于SpringBoot自定義Starter組件的資料請關注腳本之家其它相關文章!

相關文章

最新評論

虹口区| 龙门县| 敦化市| 教育| 突泉县| 湘阴县| 汝阳县| 嘉祥县| 无为县| 文登市| 大港区| 南岸区| 广州市| 突泉县| 广安市| 奈曼旗| 鹤山市| 乌审旗| 和平县| 江达县| 永兴县| 西乡县| 翁牛特旗| 阜新| 安溪县| 正宁县| 武川县| 通化县| 阿鲁科尔沁旗| 宁陕县| 通渭县| 横山县| 保德县| 辽阳县| 中西区| 龙游县| 金坛市| 乌拉特中旗| 静安区| 杂多县| 新蔡县|