日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

自定义 Spring Starter

發(fā)布時間:2023/12/20 javascript 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义 Spring Starter 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

自定義 Spring Starter

SpringBoot 中的 starter 是一種非常重要的機制,能夠拋棄以前復雜的配置,將其同意集成進 start,應用者只需要在 mave 中引入 starter 依賴。

為什么要自定義 starter

在我們的日常開發(fā)工作中,經(jīng)常會有一些獨立業(yè)務之外的配置模塊,我們經(jīng)常將其放到一個特定的包下,然后如果一個工程需要復用這塊功能的時候,需要將代碼拷貝到另一個工程,重新集成一遍,麻煩至極。如果我們將這些業(yè)務代碼之外的功能模塊封裝成一個個 starter,復用的時候只需要將其在 pom.xml 中引用依賴即可,再由 SpringBoot 為我們完成自動裝配,就非常輕松。常用案例:動態(tài)數(shù)據(jù)源、登錄模塊、基于 AOP 技術實現(xiàn)日志切面等。

自定 starter 命名規(guī)則

官方建議自定義的 starter 使用 xxx-spring-boot-starter 命名規(guī)則。一區(qū)分 SpringBoot 生態(tài)提供的 starter。

實現(xiàn)過程

  • 自定義 starter 項目
  • 使用 starter

自定義 starter 項目

  • 創(chuàng)建 maven 工程,工程名為 mf-spring-boot-starter,導入依賴:
  • <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.2.4.RELEASE</version></dependency> </dependencies>
  • 編寫 java bean
  • @EnableConfigurationProperties @ConfigurationProperties(prefix = "simplebean") public class SimpleBean {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "SimpleBean{" +"id=" + id +", name='" + name + '\'' +'}';} }
  • 編寫配置類
  • @Configuration public class MyAutoConfiguration {static {System.out.println("MyAutoConfiguration init...");}@Beanpublic SimpleBean simpleBean() {return new SimpleBean();}}
  • resource 下創(chuàng)建 /META-INF/spring.factories
  • META-INF 文件夾目錄是手動創(chuàng)建, spring.factories 也是手動創(chuàng)建的文件,在該文件中配置自己的自動配置類。

    SpringBoot 在啟動的時候會去加載我們的 simpleBean 到 IOC 容器中,這其實是一種變形的 SPI 機制。

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ mufeng.info.config.MyAutoConfiguration

    使用 starter

    配置文件

    simplebean.id=1 simplebean.name=lr

    依賴 pom

    <dependency><groupId>mufeng.info</groupId><artifactId>mf-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version> </dependency>

    代碼使用

    @Autowired private SimpleBean simpleBean;

    熱插拔技術

    新增標記類 ConfigMarker

    public class ConfigMarker {}

    新增 EnableRegisterServer 注解

    @Target({ElementType.TYPE}) // 可以作用在類、接口、枚舉 @Retention(RetentionPolicy.RUNTIME) @Import({ConfigMarker.class}) // 將某個組件類生成實例,添加到容器中 public @interface EnableRegisterServer { }

    改造 MyAutoConfiguration 新增條件注解 @ConditionalOnBean(ConfigMarker.class),@ConditionalOnBean 這個條件注解,前面的意思代表只有當前上下文含有 ConfigMarker 對象,被標注的類才能被實例化。

    @Configuration @ConditionalOnBean(ConfigMarker.class) // 在上下文中必須存在某個 bean,才會讓當前自動配置類生效 public class MyAutoConfiguration {static {System.out.println("MyAutoConfiguration init...");}@Beanpublic SimpleBean simpleBean() {return new SimpleBean();}}

    使用的時候,在啟動類上新增 @EnableRegisterServer

    總結

    以上是生活随笔為你收集整理的自定义 Spring Starter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。