欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?boot?自定義?Starter及自動配置的方法

 更新時間:2022年12月07日 14:41:37   作者:、楽.  
Starter?組件是?Spring?boot?的一個核心特性,Starter組件的出現極大的簡化了項目開發(fā),這篇文章主要介紹了Spring?boot?自定義?Starter?及?自動配置,需要的朋友可以參考下

Starter 組件簡介

Starter 組件是 Spring boot 的一個核心特性,Starter組件的出現極大的簡化了項目開發(fā),例如在項目中使用的pom.xml文件中添加以下配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.5.3</version>
</dependency>

Spring boot 就會自動關聯web 開發(fā)相關的依賴,如 tomcat 及 spring-mvc等,進而能支持web開發(fā),同時相關技術也將實現自動配置,避免了繁瑣的配置文件。

Starter 組件 使開發(fā)者不需要關注各種依賴庫的處理、不需要具體的配置信息,由Spring boot 自動完成class的發(fā)現并加載。

利用Starter實現自動化配置需要兩個條件:Maven依賴以及配置文件。

Maven依賴實際上就是導入一個JAR,然后Springboot啟動時候就會找到這個jar包中 resource/META-INF/spring.factories文件,根據文件的配置找到相關的實現類。以上就是spring boot 的自動裝配機制,如果理解該機制的話,很容易就能手寫一個 starter 組件。

對starter組件我們可以簡單理解為:

  • 每個不同的starter組件實際上完成自身的功能邏輯,然后對外提供一個bean對象讓別人調用
  • 對外提供的bean通過自動裝配機制注入到IOC容器中

自定義 Starter 組件

要實現一個自己的 starter 組件其實也很簡單,首先我們需要明確我們需要做那些事:

  • 通過配置類提供對外服務的 bean 對象
  • 按照自動裝配原理完成 spring.factories 的編寫
  • starter 自動屬性配置

Starter 組件

接下來我們就手寫一個 starter 組件,模仿 RedisTemplate,流程如下:

  • 創(chuàng)建一個spring boot 項目 redis-starter-demo
  • 創(chuàng)建MyStarterTemplate

簡單模擬 redis 的 get,set操作:

public class MyStarterTemplate {

    Map<String, String> map = new HashMap<>();

    public String put(String key, String value) {
        map.put(key, value);
        return "保存成功";
    }

    public String get(String key) {
        return map.get(key);
    }

}
  • 創(chuàng)建配置類
@Configuration
public class MyAutoConfiguration {

    @Bean
    public MyStarterTemplate myStarterTemplate(){
        return new MyStarterTemplate();
    }

}
  • 實現自動裝配流程

在 classpath 下 META-INF目錄下創(chuàng)建 spring.factories 文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.redisstarterdemo.MyAutoConfiguration

打包發(fā)布 maven install

使用 Starter

  • 創(chuàng)建一個新項目用來引入 starter test-demo
  • pom 文件引入依賴
<dependency>
    <groupId>com.example</groupId>
    <artifactId>redis-starter-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

編寫接口 ,直接 Autowired 注入MyStarterTemplate

@RestController
@RequestMapping(value = "/myRedis")
public class RedisStarterController {

    @Autowired
    private MyStarterTemplate myStarterTemplate;

    @RequestMapping("/save")
    public String save(@RequestParam String key, @RequestParam String value) {
        myStarterTemplate.put(key,value);
        return "ok";
    }

    @RequestMapping("/get")
    public String get(@RequestParam String key) {
        return  myStarterTemplate.get(key);
    }

}

以上便是一個完成的自定義 Starter,如果搞懂SPI機制的話,現在應該覺得很簡單單了吧?

Starter 傳參

以上是一個最簡單的 starter 組件,但是有這么一種情況,我們還以 redis 為例,在 redis 中我們是可以通過在配置文件中來設置地址、賬號密碼等信息的,那這種情況我們應該如何操作呢?

  • 我們還是在上面的項目中繼續(xù)添加代碼
  • 創(chuàng)建需要注入的 bean 類

這里我們通過對外暴露一個創(chuàng)建方法用來給對象傳遞地址和端口號;

public class MyRedis {

    public String say() {
        return "my host:" + host + ",port:" + port;
    }

    public static MyRedis create(String host, Integer port) {
        return new MyRedis(host, port);
    }

    public MyRedis(String host, Integer port) {
        this.host = host;
        this.port = port;
    }

    private String host;

    private Integer port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}
  • 創(chuàng)建屬性類

@ConfigurationProperties(prefix = "redis")的作用是讀取 application中以 redis 開頭的數據,還有其他幾種讀取配置文件的注解可以自行研究。

由于我們這里設置了默認值,所以如果不配置的話也不影響,會使用默認值傳遞給 bean 。

@ConfigurationProperties(prefix = "redis")
public class MyProperties {

    private String host = "127.0.0.1";

    private Integer port = 6379;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

}

@ConfigurationProperties(prefix = "redis")
public class MyProperties {

    private String host = "127.0.0.1";

    private Integer port = 6379;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

}
  • 創(chuàng)建配置類

使用配置文件,將相關信息傳遞給 bean 類。

@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class MyRedisConfiguration {

    @Bean
    public MyRedis myRedis(MyProperties properties) {
        return MyRedis.create(properties.getHost(), properties.getPort());
    }

}
  • 實現自動裝配流程
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.example.redisstarterdemo.demo1.MyAutoConfiguration,\
  com.example.redisstarterdemo.demo2.MyRedisConfiguration

在項目中引用還是同樣的操作,如果想要設置屬性的話,只需要在配置文件中編寫相關信息即可:

redis.host=196.128.255.255
redis.port=9999

自身與第三方維護

針對 spring boot 的starter 組件分為兩類:

1.springboot 自身維護的starter組件

  • 所有的 starter 組件自身不帶 spring.factories 文件,都集中在spring-boot-autoconfigure 包下的 EnableAutoConfiguration
  • springboot 裝配這些配置類是由條件的,不會將所有的都一股腦都加載進來,假設我沒用到 redis 的話就不會引入相關依賴包,這樣根據@ConditionalOnClass(RedisOperations.class)在classpath下找不到RedisOperations類,就不會加載該配置類。
  • 自身維護的starter組件的命名為:spring-boot-starter-xxx

2.第三方維護的starter 組件

  • 在自己的 jar 中維護 spring.factories 文件
  • 命名方式:xxx-spring-boot-starter

到此這篇關于Spring boot 自定義 Starter 及 自動配置的文章就介紹到這了,更多相關Spring boot 自定義 Starter內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論