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. videojs播放直播源rtmp时画面在左上角解决方案

    问题描述:https://stackoverflow.com/questions/30383135/videojs-live-rtmp-stream-player-and-video-size-iss ...

  2. java开发150个建议

    阅读目录 建议1:不要在常量和变量中出现易混淆的字母 建议2:莫让常量蜕变成变量 建议3:三元操作符的类型务必一致 建议4:避免带有变长参数的方法重载 建议5:别让null值和空值威胁到变长方法 建议 ...

  3. 首次使用AndroidStudio创建hello world遇到的坑!(Mac系统下)

    第一次用AS,且不说它SDK配置的琐碎,光建立第一个简单的HelloWorld项目的配置包问题就把我卡了一天.这个坑必须记录一下,

  4. jmeter 测试MD5加密登录接口

    1.点击options-funciton helper dialog,打开函数助手 在string to  calculate MD5 hash 中填写密码,复制function sysntax中的值 ...

  5. s21day19 python笔记

    s21day19 python笔记 一.面向对象的基本知识 1.1 基本格式 # 定义类 class 类名: def 方法名(self,name): print(name) return 123 de ...

  6. 枚举转map

    import java.util.HashMap; import java.util.Map; public enum PayType { ALIPAY("支付宝扫码", 15), ...

  7. Python3常用函数、方法总结(持续更新…)

    最近刷LeetCode,自己自娱自乐完之后去discussion看大佬们的各种巧妙解法,总是止不住的双击666--加上最近Python3用的比较多(虽然Python实在不推荐跑算法题目,一是运行效率太 ...

  8. javaee设计模型简介

    (一)五种模式 1.单例模式 在某些情况下,有些对象只需要一个就可以了,即每个类只需要一个实例.例如,一台计算机上的可以连接多台打印机,但是该计算机上的打印程序只能有一个,这里就可以通过单例模式来避免 ...

  9. JavaScript作用域(第七天)

    我们都知道js代码是由自上而下的执行,但我们来看看下面的代码: test(); function test(){ console.log("hello world"); }; 如果 ...

  10. JavaScript 查找图中连接两点的所有路径算法

    1.把图看成以起点为根节点的树 2.使用深度遍历算法遍历路径 3.遍历到节点为目标节点时,保存这条路径 find2PointsPath(sourceId, targetId) { const { no ...