Spring @Value注解 and Spring Boot @ConfigurationProperties注解
一、Spring的@Value
Spring支持在XML配置文件和注解中使用Spring EL(Spring 表达式语言),Spring EL类似于JSP的EL表达式语言。
在Spring开发中经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等,我们可以使用Spring的表达式语言实现资源的注入。
程序代码里,Spring主要在@Value注解的参数中使用EL表达式。
- 注入普通字符串
- 注入操作系统属性
- 注入表达式运算结果
- 注入其他Bean的属性
- 注入文件内容
- 注入网址内容
- 注入属性文件(注意:用的是$符号)
import org.springframework.core.io.Resource;
import org.springframework.core.env.Environment;
import org.apache.commons.io.IOUtils; @Component
public class ELConfig {
@Value("注入普通字符串")// 注入普通字符串
private String normal; @Value("#{systemProperties['os.name']}")// 注入操作系统属性
private String osName; @Value("#{T(java.lang.Math).random() * 100.0 }")// 注入表达式结果
private double randomNumber; @Value("#{payOrderQueryController.payCenterFacade}")// 注入其他Bean属性
private IPayCenterFacade fromAnother; @Value("classpath:test.txt")// 注入文件资源
private Resource testFile; @Value("https://www.baidu.com")// 注入网址资源
private Resource testUrl; @Value("${book.name}")// 注入配置文件【注意是$符号】
private String bookName; @Autowired// Properties可以从Environment获得
private Environment environment; @Override
public String toString() {
try {
return "ELConfig [normal=" + normal
+ ", osName=" + osName //os.name,如Windows 8.1
+ ", randomNumber=" + randomNumber //值如97.53293482705482
+ ", fromAnother=" + fromAnother //别的bean的成员属性
+ ", testFile=" + IOUtils.toString(testFile.getInputStream()) //输出文件里的内容
+ ", testUrl=" + IOUtils.toString(testUrl.getInputStream()) //输出网页的html
+ ", bookName=" + bookName //配置的值
+ ", environment=" + environment.getProperty("book.name") + "]";
} catch (IOException e) {
e.printStackTrace();
return null;
}
} }
二、Spring Boot的@ConfigurationProperties
先看下面的@Value注解:
@Value("${book.name}")
private String bookName;
@Value("${book.author}")
private String bookAuthor;
上面这种使用@Value注入每个配置在实际项目中会显得格外麻烦,因为我们的配置通常会是许多个,就要使用@Value注入很多次。
Spring Boot提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个Bean关联,从而实现类型安全的配置。
@Component
@ConfigurationProperties(prefix = "book")
public class Book { private String name;
private String author;
private int age; //get.. set..
}
application.yml配置:
book:
name: 一个人的朝圣
author: 蕾秋·乔伊斯
age: 35
@ConfigurationProperties有两个属性
- prefix:指定properties的配置的前缀
- locations:指定properties文件的位置
Spring @Value注解 and Spring Boot @ConfigurationProperties注解的更多相关文章
- spring学习笔记(一)@ConfigurationProperties注解
结论: 这个注解主要是为了将配置文件中的属性映射到实体类上,并且支持嵌套映射. 代码说明: @ConfigurationProperties(prefix = "person") ...
- spring 、spring boot 常用注解
@Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...
- 在Spring Boot中使用 @ConfigurationProperties 注解
但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...
- 在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties
但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...
- Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置
从spring-boot开始,已经支持yml文件形式的配置,@ConfigurationProperties的大致作用就是通过它可以把properties或者yml配置直接转成对象 @Componen ...
- Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性
在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...
- Spring Boot中@ConfigurationProperties注解实现原理源码解析
0. 开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis ...
- 精尽Spring Boot源码分析 - @ConfigurationProperties 注解的实现
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- Spring Boot中注解@ConfigurationProperties
在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...
随机推荐
- Html吸顶效果
Html吸顶效果 一.HTML HTML中需要给div一个id <!DOCTYPE html> <html lang="en"> <head> ...
- Installing Precise (12.04.2) using netboot onto a Marvell ArmadaXP Development Board
https://wiki.ubuntu.com/ARM/Server/Install https://wiki.ubuntu.com/ARM/Server/Install/ArmadaXP Arm ...
- 子分区 复合分区 Subpartitioning
https://dev.mysql.com/doc/refman/8.0/en/partitioning-subpartitions.html
- 在SVNX中实现$Ids的自动替换
在PHPstorm中实现$Ids的自动替换 ①.打开SVN的配置文件. [miscellany] enable-auto-props = yes [auto-props] .c = svn:keywo ...
- expect 自动完成交互式程序神器
expect 安装 yum -y install expect # Centossudo apt-get install expect # Ubuntu 作用:自动填写交互式程序的值. 原理:通过读取 ...
- js对json格式对象进行增加,修改,删除
// 假设数据为data var data=[ { "ID":"83d349de-eca4-4974-a8a7-f9b44b48c6f2", "IsL ...
- session操作类
using System;using System.Web; /// <summary> ///session操作类 /// </summary> public class a ...
- es6 学习二 Generator
安装babel的拓展包(Polyfill) ,对Generator的转义 这是一个补完babel支持es6的拓展包,配置步骤为3个: 打开命令行键入 npm install --save-dev ba ...
- Servlet----------通过 GenericServlet 开发Servlet
通过继承GenericServlet抽象类.只需要重写service()方法即可.(这个知识点了解即可) package cn.servlet; import java.io.IOException; ...
- python-面向对象-06_私有属性和私有方法
私有属性和私有方法 01. 应用场景及定义方式 应用场景 在实际开发中,对象 的 某些属性或方法 可能只希望 在对象的内部被使用,而 不希望在外部被访问到 私有属性 就是 对象 不希望公开的 属性 私 ...