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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

详细介绍注解@ConfigurationProperties使用

發布時間:2025/1/21 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 详细介绍注解@ConfigurationProperties使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 一、@ConfigurationProperties介紹
      • 一、@ConfigurationProperties使用詳情

寫在前面:
我是「境里婆娑」。我還是從前那個少年,沒有一絲絲改變,時間只不過是考驗,種在心中信念絲毫未減,眼前這個少年,還是最初那張臉,面前再多艱險不退卻。
寫博客的目的就是分享給大家一起學習交流,如果您對 Java感興趣,可以關注我,我們一起學習

前言:前兩天在做項目時候,一個需求是要求兩個bean啟動要互斥,后來經過一番查找發現SpringBoot這個注解@ConfigurationProperties可以實現此功能。經過一番學習來分享給大家。

一、@ConfigurationProperties介紹

@ConditionalOnProperty一個最大的好處是可以控制@Configuration是否生效。
@ConditionalOnProperty屬性介紹:

String[] value() default {}; //數組,獲取對應property名稱的值,與name不可同時使用 String prefix() default "";//property名稱的前綴,可有可無 String[] name() default {};//數組,property完整名稱或部分名稱(可與prefix組合使用,組成完整的property名稱),與value不可同時使用 String havingValue() default "";//可與name組合使用,比較獲取到的屬性值與havingValue給定的值是否相同,相同才加載配置 boolean matchIfMissing() default false;//缺少該property時是否可以加載。如果為true,沒有該property也會正常加載;反之報錯

@ConditionalOnProperty如何控制@Configuration是否生效,具體操作是通過其兩個屬性name以及havingValue來實現的。

namehavingValue
truetrue
falsefalse
truefalse
falsetrue

name和havingValue屬性相同的時候@Configuration生效,否則不生效。

一、@ConfigurationProperties使用詳情

1、在application.properties添加配置文件

myapp.mail.localPath=zsl myapp.mail.enable=true

2、創建兩個配置類

Conditional1Configuration配置類

@Configuration @ConfigurationProperties(prefix = "myapp.mail") @ConditionalOnProperty(prefix = "myapp.mail",name = "enable",havingValue = "false") public class Conditional1Configuration {private String localPath;private Boolean enable;@Beanpublic Conditional1 test1() {Conditional1 test = new Conditional1();test.setEnable(enable);test.setUser(localPath);return test;}public String getLocalPath() {return localPath;}public void setLocalPath(String localPath) {this.localPath = localPath;}public Boolean getEnable() {return enable;}public void setEnable(Boolean enable) {this.enable = enable;} }

ConditionalConfiguration配置類

@Configuration @ConfigurationProperties(prefix = "myapp1.mail") @ConditionalOnProperty(prefix = "myapp.mail",name = "enable",havingValue = "true",matchIfMissing = true) public class ConditionalConfiguration {@Value("${myapp.mail.enable}")private Boolean enable;private String localPath;@Beanpublic Conditional test2() {Conditional test = new Conditional();test.setEnable(enable);test.setUser(localPath);return test;}public Boolean getEnable() {return enable;}public void setEnable(Boolean enable) {this.enable = enable;}public String getLocalPath() {return localPath;}public void setLocalPath(String localPath) {this.localPath = localPath;} }

當我把配置文件myapp.mail.enable改為true時候這個配置ConditionalConfiguration啟動,當改為false時候 Conditional1Configuration啟動。

至此,我們怎么使用注解@ConfigurationProperties介紹完畢。
如果想更詳細查看以上所有代碼請移步到github:如何使用注解@ConfigurationProperties

總結

以上是生活随笔為你收集整理的详细介绍注解@ConfigurationProperties使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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