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

歡迎訪問 生活随笔!

生活随笔

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

javascript

SpringBoot自定义starter

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

目錄

  • 前言
  • `SpringBoot` 自定義 `starter`
    • 創建多模塊項目
    • 項目編碼
      • `custom-spring-boot-starter` 父模塊 `Maven` 依賴
      • `hello-spring-boot-starter-autoconfigure` 子模塊
        • `HelloProperties` 屬性映射類
        • `HelloService` 業務邏輯類
        • `HelloServiceAutoConfiguration` 配置類
        • 創建 `spring.factories` 文件
        • `Maven` 打包
    • 自定義 `starter` 的使用
      • `Maven` 依賴
      • `application.properties` 配置文件
      • `HelloController` 測試接口類
      • 測試結果

前言

今天看到了一道面試題,如何實現一個自定義的 SpringBoot starter,或者說,SpringBoot 如何加載我們自己的 jar 包?其實,仔細想想本質是考察 SpringBoot 的自動配置原理,了解了其自動配置原理即可實現 SpringBoot 自定義 starter

SpringBoot 自動配置原理:https://blog.csdn.net/weixin_38192427/article/details/115178359

SpringBoot 自定義 starter

創建多模塊項目

SpringBoot 的官方 starter 命名方式為 spring-boot-starter-xxx 如 spring-boot-starter-quartz,SpringBoot 官方建議第三方 starter 的命名方式一般為 xxx-spring-boot-starter

創建一個 custom-spring-boot-starter 項目,項目下面新建兩個模塊

  • hello-spring-boot-starter(場景啟動器,普通 Maven 工程)
  • hello-spring-boot-starter-autoconfigure(自動配置包,需用到 Spring Initializr 創建的 Maven 工程)

項目編碼

custom-spring-boot-starter 父模塊 Maven 依賴

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.9.RELEASE</version><relativePath></relativePath> </parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-actuator-autoconfigure</artifactId></dependency> </dependencies>

hello-spring-boot-starter-autoconfigure 子模塊

HelloProperties 屬性映射類

@ConfigurationProperties(prefix = "hello") public class HelloProperties {private String username;private String password;// 省略 get/set 方法...... }
  • @ConfigurationProperties:負責將配置文件里的配置信息綁定映射到實體 bean 上

HelloService 業務邏輯類

public class HelloService {private String username;private String password;public String haloHello() {return "用戶名為:" + username + ">>>" + "密碼為:" + password;}// 省略 get/set 方法...... }

HelloServiceAutoConfiguration 配置類

@Configuration @EnableConfigurationProperties(value = HelloProperties.class) @ConditionalOnClass(value = HelloService.class) @ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true) public class HelloServiceAutoConfiguration {@Autowiredprivate HelloProperties helloProperties;@Bean@ConditionalOnMissingBean(value = HelloService.class)public HelloService helloService() {System.out.println("Execute Create New Bean");HelloService helloService = new HelloService();helloService.setUsername(helloProperties.getUsername());helloService.setPassword(helloProperties.getPassword());return helloService;} }
  • @EnableConfigurationProperties(value = HelloProperties.class):負責將指定的 bean 注冊到Spring 容器中
  • @ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true):當配置文件中 hello.enabled = true 時進行自動配置,如果沒有設置此值就默認使用 matchIfMissing 指定的值

@Conditional 條件注解

  • @ConditionalOnBean:當給定的 bean 存在時,則實例化當前 bean
  • @ConditionalOnMissingBean:當給定的 bean 不存在時,則實例化當前 bean
  • @ConditionalOnClass:當給定的類名在類路徑上存在時,則實例化當前 bean
  • @ConditionalOnMissingClass:當給定的類名在類路徑上不存在時,則實例化當前 bean
  • @ConditionalOnExpression:當表達式為 true 的時候,才會實例化一個 bean

創建 spring.factories 文件


spring.factories 文件內容如下

# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.example.config.HelloServiceAutoConfiguration
  • 如果有多個自動配置類,則用,隔開
  • ,后不要有空格之類的符號,就只寫該類的全路徑,不然會出現無法找到此 bean 的錯誤

Maven 打包

用 Maven 插件,將該工程 install 到本地 Maven 倉庫。記得要把 hello-spring-boot-starter-autoconfigure 的 test 包刪除

自定義 starter 的使用

Maven 依賴

<dependencies><!--引入自定義的starter--><dependency><groupId>org.example</groupId><artifactId>hello-spring-boot-starter-autoconfigure</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!--lombok 插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><scope>provided</scope></dependency> </dependencies>

application.properties 配置文件

在項目 hello-spring-boot-starter 中添加配置文件 application.properties

server.port=8080hello.username=xiaoming hello.password=123123

HelloController 測試接口類

@Slf4j @Controller public class HelloController {@Autowiredprivate HelloService helloService;@Autowiredprivate HelloProperties helloProperties;@GetMapping(path = "/hello")@ResponseBodypublic String sayHello() {log.info("用戶名為:" + helloProperties.getUsername());log.info("密碼為:" + helloProperties.getPassword());return helloService.haloHello();} }

測試結果

控制臺結果


postman 接口返回結果

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。