Spring @Value注解
Spring @Value注解的用法。
基本使用
1
("${user.name}")
SpELl方式
systemProperties是spring自动注入的系统变量集合,也可以使用自己注入的变量集合。
1
("#{systemProperties['user.name']}")
设置默认值
1
("${user.name:root}")
SpEl方式设置默认值
1
("#{systemProperties['user.name'] ?: 'root'}") //注意字符串要加单引号
设置null值
1
2
3("${user.name:#{null}}") //混合方式
("#{systemProperties['user.name'] ?: null}") //纯SpEl方式
检查是否为null或者空字符串
1
(value = "#{systemProperties['user.name'] == null ? null : ''.equals(systemProperties['user.name']) ? null : systemProperties['user.name']}")