在Spring Boot中使用 @ConfigurationProperties 注解
但 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;
// ... 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。
方案1:
@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时候被发现了。
总结:
@ConfigurationProperties很方便使用。 比用@Value注解好吗? 在特定的方案中是的,这只是一个选择问题。
作者:crocodile_b
链接:http://www.jianshu.com/p/df57fefe0ab7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
在Spring Boot中使用 @ConfigurationProperties 注解的更多相关文章
- 在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties
但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...
- Spring Boot 中使用 @ConfigurationProperties 注解
@ConfigurationProperties 主要作用:绑定 application.properties 中的属性 例如: @Configuration public class DataSou ...
- 在Spring Boot中使用 @ConfigurationProperties 注解 (二十六)
@ConfigurationProperties主要作用:就是绑定application.properties中的属性 java代码 @Configuration public class DataS ...
- 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数
使用 @Value("${property}") 注释注入配置属性有时会很麻烦,尤其是当你使用多个属性或你的数据是分层的时候. Spring Boot 引入了一个可替换的方案 -- ...
- Spring Boot中使用MyBatis注解配置详解(1)
之前在Spring Boot中整合MyBatis时,采用了注解的配置方式,相信很多人还是比较喜欢这种优雅的方式的,也收到不少读者朋友的反馈和问题,主要集中于针对各种场景下注解如何使用,下面就对几种常见 ...
- 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Spring Boot 中使用 @Transactional 注解配置事务管理
事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP ...
- Spring Boot中使用@Transactional注解配置事务管理
事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP ...
- Spring boot中相关的注解
一.相关类中使用的注解 @RestController:REST风格的控制器 @RequestMapping:配置URL和方法之间的映射 @SpringBootApplication:应用程序入口类 ...
随机推荐
- MTK平台-抓取蓝牙log
一.MTKLOG抓取 .在拔号键盘输入暗码 *#*##*#* 进入工模EngineerMode .在 Log and Debugging -> MTKLogger 点击开始 .MTKLog存储路 ...
- Android Studio 入口程序的设置方法
在src -> main中 ,打开 AndroidManifest.xml 这个文件 下面这里有两个窗口,如果要想把哪个窗口设置成入口窗体,只要把下面红色的放在这个节点中就可以了 <act ...
- 完美解决win10家庭版本系统无法远程连接问题
版权声明:本文转载.原文: https://blog.csdn.net/rainmaple20186/article/details/80913191 近期接入同一局域网的服务器,发现在连接的时候,报 ...
- LeetCode第一次刷题
感觉自身编程水平还是差很多,所以刷刷题 LeetCode貌似是一个用的人比较多的题库,下面是第一题 给数组和目标和求需要元素的下标 public class Solution { public int ...
- spring源码1:基本概念
一.预习 1.如何用spring?零配置(注解)或少配置,与应用无侵入性一起运行,与主流框架无缝集成. 2.spring 是什么?spring 是 java 企业应用级框架,目的是为了简化开发:主要体 ...
- Spring Boot 定时任务使用
详情参考文章https://blog.csdn.net/qq_31001665/article/details/76408929
- 终于不再在懵逼mysql原生语句,orm超级登场
import sqlalchemy from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import cre ...
- ssm项目整合shiro
pom.xml <properties> <shiro.version>1.2.2</shiro.version> </properties> < ...
- JavaScript杂谈(第六天)
js中可以使用Function创建函数 var func=new Function(); 这个对象可以将字符串转换为函数 var func=new Function("console.wri ...
- https://www.cnblogs.com/yudanqu/p/9467803.html
https://www.cnblogs.com/yudanqu/p/9467803.html