@Value 真是一个神奇的注解
变量直接赋值
/* 直接设置值 */
@Value("25")
private int hight;
@Value("65")
private Integer wight;
@Value("human")
private String species;
Resource读取文件
注意 这里的Resource是spring的核心类 不是javax的Resource
import org.springframework.core.io.Resource;
// 设置系统文本内容
@Value("classpath:mock/resume.txt")
private Resource resume;
// 设置url 调用后返回的值
@Value("http://www.baidu.com")
private Resource baidu;
${}
/* 使用$设置值 */
@Value("${user.password}")
private String password;
#{}
/* 使用#设置值 */
// 1. 使用systemProperties[‘xxx’]获取系统参数
@Value("#{systemProperties['os.name']}")
private String osName;
// 2. 调用系统方法
@Value("#{T(Math).random() * 100.0}")
private double waterContent;
链接参考
Spring Boot系列四 Spring @Value 属性注入使用总结一
spring中的SpEL表达式