@Component
public class Person {
@Value("#{11*20}")
private int age;
@Value("true")
private boolean merry;
@Value("${person.name}")
private String name;
}
1、@Value("#{}") Spring 表达式语言(简称SpEL)
SpEL 字面量:
- 整数:#{8}
- 小数:#{8.8}
- 科学计数法:#{1e4}
- String:可以使用单引号或者双引号作为字符串的定界符号。
- Boolean:#{true}
SpEL引用bean , 属性和方法:
- 引用其他对象:#{car}
- 引用其他对象的属性:#{car.brand}
- 调用其它方法 , 还可以链式操作:#{car.toString()}
- 调用静态方法静态属性:#{T(java.lang.Math).PI}
SpEL支持的运算符号:
- 算术运算符:+,-,*,/,%,^(加号还可以用作字符串连接)
- 比较运算符:< , > , == , >= , <= , lt , gt , eg , le , ge
- 逻辑运算符:and , or , not , |
- if-else 运算符(类似三目运算符):?:(temary), ?:(Elvis)
- 正则表达式:#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}
@Value("true") 直接赋值
@Value("${person.name}") //从配置文件application.properties
中取值