惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - Sun_china

服务注册中心,Eureka比Zookeeper好在哪里? 数据库锁 【转】数据库连接池优化配置(druid,dbcp,c3p0) scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别 Shiro简介——《跟我学Shiro》 【转】Lombok 安装、入门 - 消除冗长的 java 代码 java中抽象类是否可以继承实体类? MyBatis中使用#和$书写占位符有什么区别? 什么是悲观锁和乐观锁? java为什么不能根据返回值重载? java中String、StringBuffer、StringBuilder的区别 【转】java面试题 分布式系统中关于转账案例的剖析 - Sun_china 支付平台架构师谈大规模高并发服务化系统设计经验 - Sun_china 全面了解Ngnix的主要应用的场景 - Sun_china HashMap和HashSet的区别 - Sun_china hashmap和hashtable的区别? - Sun_china java多线程中并发集合和同步集合有哪些?区别是什么? - Sun_china java多线程中最佳的实践方案是什么? - Sun_china
在Spring Boot中使用 @ConfigurationProperties 注解
Sun_china · 2017-07-22 · via 博客园 - Sun_china

Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean。 这里会介绍如何使用@ConfigurationProperties
继续使用mail做例子。配置放在mail.properties文件中。属性必须命名规范才能绑定成功。举例:
1 protocol and PROTOCOL will be bind to protocol field of a bean
2 smtp-auth , smtp_auth , smtpAuth will be bind to smtpAuth field of a bean
3 smtp.auth will be bind to … hmm to smtp.auth field of a 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;  
    

…从如下属性中创建 ( 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。

方案1:
@Configuration
@ConfigurationProperties(locations = "classpath:mail.properties", 
                         prefix = "mail")
public class MailConfiguration { 
  public static class Smtp {
    private boolean auth;
    private boolean starttlsEnable;
    
方案2

我们和上面例子一样注解属性,然后用 Spring的@Autowire来注入 mail configuration bean:

@Configuration
@EnableConfigurationProperties(MailProperties.class)
 public class MailConfiguration { 
    @Autowired private MailProperties mailProperties; 

    @Bean public JavaMailSender javaMailSender() {
      

请注意@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时候被发现了。

总结:

@ConfigurationProperties很方便使用。 比用@Value注解好吗? 在特定的方案中是的,这只是一个选择问题。

作者:crocodile_b
链接:http://www.jianshu.com/p/df57fefe0ab7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。