在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:应用程序入口类 ...
随机推荐
- 炸金花游戏(4)--炸金花AI基准测试评估
前言: 本文将谈谈如何评估测试炸金花的AI, 其实这个也代表一类的问题, 德州扑克也是类似的解法. 本文将谈谈两种思路, 一种是基于基准AI对抗评估, 另一种是基于测试集(人工选定牌谱). 由于炸金花 ...
- 利用Python进行数据处理1——学会使用NumPy
一.学会使用ndarray 1.1什么是ndarray? ndarray是NumPy中的一种多维数组对象,他可以是一维的.二维的.甚至更多维次.当然创建更多维次的数组并不是他的优点所在,他的优点在于它 ...
- 复杂透视表的SQL生成方法
一般而言,利用表单查看数据时,会从不同的维度来涉及透视表.比如,从产品和时间维度分析销售数据. 当需要从时间维度去分析时,同时希望能有同比,环比数据,那么将时间维度设计成列将极大方便SQL的编写. 如 ...
- jmeter 上传附件脚本报Non HTTP response code: java.io.FileNotFoundException
如果上传附件报如下错误,就需要把附件放到和脚本同一路径下就解决了
- linux btrfs文件系统管理与应用
btrfs文件系统管理与应用 1.btrfs文件系统 基本介绍 btrfs文件系统在CentOS7.x上属于技术预览版 btrfs文件系统英文名:B-tree FileSystem或者Butter ...
- APP测试常用工具以及框架
APP测试常用工具以及框架 1)纯白盒方式的测试,Monkey.一般是开发用的比较多,动手能力强的同学可以自己去尝试下! 2)偏白盒的robotium,这家伙号称是黑盒,但是本人不太认同- 因为使用r ...
- 使用User Primary Email作为GUID的问题
最近发现有人使用CRM的user primary email作为GUID, 并且做了plugin来控制user primary email. 这样做法是非常有问题而且会影响同名的再次注册的用户. 假如 ...
- 面试题之python基础
基础语法 输入和输出 代码中要修改不可变的数据会出现什么问题,抛出什么异常? 代码不会征程运行,抛出TypeError异常 a = 1,b = 2,不用中间变量交换a和b的值? # 方法1 a = a ...
- Scaffold(Material库中提供的页面脚手架)知识点
Scaffold 包含:appBar.body.floatingActionButton
- cellmap 基站查询 for android
cellmap for android 3.6.8.8.1.8 更新日期:2019年4月28日 特别声明:本软件不能进行手机定位,不能对手机号码定位,谨防被骗. 安装说明:请卸载旧版本后,重新下载安装 ...