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

歡迎訪問 生活随笔!

生活随笔

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

javascript

在Spring Boot中使用 @ConfigurationProperties 注解

發布時間:2024/7/5 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring Boot中使用 @ConfigurationProperties 注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@ConfigurationProperties根據類型校驗和管理application中的bean。

Spring Boot 使用一些松的規則來綁定屬性到@ConfigurationProperties?bean 并且支持分層結構(hierarchical structure)。
開始創建一個@ConfigurationProperties?bean:

@ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") public class MailProperties { public static class Smtp { private boolean auth; private boolean starttlsEnable; // ... getters and setters }@NotBlank private String host;private int port; private String from; private String username;private String password; @NotNull private Smtp smtp; // ... getters and setters }

?mail.properties 文件

mail.host=localhost mail.port=25 mail.smtp.auth=false mail.smtp.starttls-enable=false mail.from=me@localhost mail.username= mail.password=

上例中我們用@ConfigurationProperties注解就可以綁定屬性了。ignoreUnknownFields = false告訴Spring Boot在有屬性不能匹配到聲明的域的時候拋出異常。開發的時候很方便!?prefix?用來選擇哪個屬性的prefix名字來綁定。
請注意setters 和 getters 需要在@ConfigurationProperties?bean中創建! 與@Value注解相反, 這帶來了代碼中的一些困擾 (特別是簡單的業務中,個人觀點).
OK,但是我們需要用屬性來配置 application. 有至少兩種方式來創建@ConfigurationProperties。即可以搭配@Configuration?注解來提供 @Beans 也可以單獨使用并注入 @Configuration bean。

@Configuration @ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail") public class MailConfiguration { public static class Smtp {private boolean auth;private boolean starttlsEnable;// ... getters and setters }@NotBlank private String host; private int port;private String from; private String username;private String password; @NotNull private Smtp smtp; // ... getters and setters @Bean public JavaMailSender javaMailSender() {// omitted for readability } }
方案2

我們和上面例子一樣注解屬性,然后用 Spring的@Autowire來注入 mail configuration bean:

@Configuration @EnableConfigurationProperties(MailProperties.class)public class MailConfiguration { @Autowired private MailProperties mailProperties; @Bean public JavaMailSender javaMailSender() {// omitted for readability }}

請注意@EnableConfigurationProperties注解。 這個注解告訴Spring Boot?使能支持@ConfigurationProperties。如果不指定會看到如下異常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [demo.mail.MailProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

注意: 還有其他辦法 (Spring Boot 總是有其他辦法!) 讓@ConfigurationProperties?beans 被添加 – 用@Configuration或者?@Component注解, 這樣就可以在 component scan時候被發現了。
不過也有采用讀文件的方式去做的

new ConfigTool().getClass().getResourceAsStream("/config.properties");

這個就不詳細展開了

spring.profiles.active=testdb application.propreries 里面可以這樣配置分支來區分線上 @Profile("testdb")

然后通過這個注解去執行

?

轉載于:https://www.cnblogs.com/zhujiabin/p/9856876.html

總結

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

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