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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Boot自定义Start组件开发

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

Start 組件開發,核心是自動注解類的注解順序,即根據條件進行注解。

首先我們來理解@Configuration、@Value、@Bean注解

以DataSourceAutoConfiguration類注解為例,
@Configuration表名此類是個配置類;
@ConditionalOnClass表明當DataSource.class, EmbeddedDatabaseType.class存在時才會繼續進行下面的配置;@EnableConfigurationProperties從IOC容器中獲取application.properties配置文件的Bean;
@Import 導入相關類。

@ConfigurationProperties注解主要用來把properties配置文件轉化為bean來使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是獲取不到properties配置文件轉化的bean的。
@EnableConfigurationProperties詳細說明

自動加載核心注解說明

有了以上的了解后,來創建 Maven 項目, 目錄結構如下:

添加pom.xml依賴(根據組件功能添加)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><version>2.0.0.RELEASE</version><optional>true</optional></dependency></dependencies>

在使用Spring官方的Starter時通常可以在application.properties中來配置參數覆蓋掉默認的值。即sex的值會被配置文件中的值替換掉。
PersonProperties 類

@ConfigurationProperties(prefix = "spring.person") public class PersonProperties {// 姓名private String name;// 年齡private int age;// 性別private String sex = "M";// Getter & Setter}

PersonService類

public class PersonService {private PersonProperties properties;public PersonService() {}public PersonService(PersonProperties properties) {this.properties = properties;}public void sayHello(){System.out.println("大家好,我叫: " + properties.getName() + ", 今年" + properties.getAge() + "歲"+ ", 性別: " + properties.getSex());} }

PersonServiceAutoConfiguration 類

@Configuration @EnableConfigurationProperties(PersonProperties.class) @ConditionalOnClass(PersonService.class) @ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true) public class PersonServiceAutoConfiguration {@Autowiredprivate PersonProperties properties;@Bean@ConditionalOnMissingBean(PersonService.class) // 當容器中沒有指定Bean的情況下,自動配置PersonService類public PersonService personService(){PersonService personService = new PersonService(properties);return personService;} }

spring.factories文件
注意:META-INF是自己手動創建的目錄,spring.factories也是手動創建的文件,在該文件中配置自己的自動配置類

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.cnbi.PersonServiceAutoConfiguration

最后將項目打包 mvn clean install

在另一個項目中添加依賴 如何在項目中添加本地jar包

<dependency><groupId>cnbi</groupId><artifactId>helloworld-spring-boot-starter</artifactId><version>1.0</version></dependency>

配置application.properties

spring.person.age=23 spring.person.name=ss spring.person.sex=F

啟動測試類

@RunWith(SpringRunner.class) @SpringBootTest public class LarkApplicationTests {@Autowired@SuppressWarnings("ALL")private PersonService personService;@Testpublic void testHelloWorld() {personService.sayHello();}}

2019-09-11 09:25:19.453 INFO 7272 — [ main] com.tiamo.lark.LarkApplicationTests : Started LarkApplicationTests in 4.893 seconds (JVM running for 6.7)

大家好,我叫: ss, 今年23歲, 性別: F

總結

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

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