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 注解的更多相关文章

  1. 在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties

    但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...

  2. Spring Boot 中使用 @ConfigurationProperties 注解

    @ConfigurationProperties 主要作用:绑定 application.properties 中的属性 例如: @Configuration public class DataSou ...

  3. 在Spring Boot中使用 @ConfigurationProperties 注解 (二十六)

    @ConfigurationProperties主要作用:就是绑定application.properties中的属性 java代码 @Configuration public class DataS ...

  4. 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数

    使用 @Value("${property}") 注释注入配置属性有时会很麻烦,尤其是当你使用多个属性或你的数据是分层的时候. Spring Boot 引入了一个可替换的方案 -- ...

  5. Spring Boot中使用MyBatis注解配置详解(1)

    之前在Spring Boot中整合MyBatis时,采用了注解的配置方式,相信很多人还是比较喜欢这种优雅的方式的,也收到不少读者朋友的反馈和问题,主要集中于针对各种场景下注解如何使用,下面就对几种常见 ...

  6. 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  7. Spring Boot 中使用 @Transactional 注解配置事务管理

    事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP ...

  8. Spring Boot中使用@Transactional注解配置事务管理

    事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP ...

  9. Spring boot中相关的注解

    一.相关类中使用的注解 @RestController:REST风格的控制器 @RequestMapping:配置URL和方法之间的映射 @SpringBootApplication:应用程序入口类 ...

随机推荐

  1. 【软件安装与环境配置】TX2安装配置caffe过程

    Tx2刷机 sudo sh ./caffe_dependency.sh 注意python的版本问题. 问题 LD -o .build_release/lib/libcaffe.so. /usr/bin ...

  2. Mysql远程访问报错2003

    如果没有权限新建一个mysql用户给添加远程连接的权限(推荐设置) 1.例如,你想admin使用123456从任何主机连接到mysql服务器的话. GRANT ALL PRIVILEGES ON *. ...

  3. git-本地仓库和远程仓库关联

    以github为例: 在github创建仓库 本地新建项目后执行:git init 将远程项目和本地项目关联:git remote add origin +远程仓库地址 如:git remote ad ...

  4. docker安装mysql容器后,是用navicat连接报client does not support authentication protocol requested by server consider upgrading mysql client

    #进入容器 docker exec -it mysql bash#进入mysqlmysql -u root -p#重置密码ALTER USER 'root'@'%' IDENTIFIED WITH m ...

  5. Centos中安装gitlab

    安装依赖: sudo yum install curl openssh-server openssh-clients postfix cronie sudo service postfix start ...

  6. python_ 基本语法

    一.基础知识: 1.鸡汤 摘抄至 :简明 python 教程 在人生中取得成功,与其说靠天才与机会,不如说靠专注与毅力! Python 特点:简单.易于学习(简单得语法体系).自由且开发.高级语言.跨 ...

  7. leetcode 刷题(1)--- 两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...

  8. java第二章总结与感想

    本章主要介绍Java程序设计环境,下面一节一节的记录: 2.1 安装java工具箱(JDK): 2.1.1, 下载JDK: 这一节主要介绍了以下知识点: (1)jdk的下载地址: (2)一些java术 ...

  9. mysql的分表与分区的区别

    http://www.2cto.com/database/201503/380348.html

  10. js中的call与apply随笔

    关于javascript中的call方法.总结网上的观点: 1: 继承. 2: 修改函数运行时的this指针. 可能说起来还是比较抽象,下面具体例子说一下: x.call(y) (1)y.m = x  ...