Spring EL 表达式是Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL,JSTL表达式语言。
使用Spring表达式语言,我们在项目中不需要手动管理Spring表达式的相关的接口和实例,只需要直接编写Spring表达式,Spring就会自动解析并转换表达式。
简单字符串注入:
@Value("spring boot")
private String spring;
从系统属性中获取并注入:
@Value("#{systemProperties['os.name']}")
private String osName;
调用类的方法:
@Value("#{T(java.lang.Math).random() * 100}")
private double randomNumber;
链式调用方法:
@Value("#{person.getpName().toUpperCase()}")
private String pName;
算术运算:
@Value("#{(1+2)>3}")
public boolean count;
三元运算符:
@Value("#{(1+2)>3 ? 'yes' : 'no'}")
public String count;