javascript
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 文件內容如下
- 如果有多個自動配置類,則用,隔開
- ,后不要有空格之類的符號,就只寫該類的全路徑,不然會出現無法找到此 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=123123HelloController 測試接口類
@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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言贪心算法书包问题,贪心算法背包有关
- 下一篇: gradle idea java ssm