一、${}
${} 用于加载外部文件指定的Key值,常用在xml中,@Value(" ${key_value}")
二、#{}
#{} 功能比 ${} 功能更强大,强调的是把内容赋值给属性,示例:
- 表示常量: #{‘1’} ,#{’ This is a Constant Str’}
- 使用java代码new/instance of: 此方法只能是java.lang 下的类才可以省略包名 #{“new Spring(‘Hello World’)”}
- 使用T(Type): 使用“T(Type)”来表示java.lang.Class实例,同样,只有java.lang 下的类才可以省略包名。此方法一般用来引用常量或静态方法 ,#{“T(Integer).MAX_VALUE”}
- 变量: 使用“#bean_id”来获取,#{“beanId.field”}
- 方法调用: #{“#abc.substring(0,1)”}
- 运算符表达式: 算数表达式,比较表达式,逻辑表达式,赋值表达式,三目表达式,正则表达式
- 判断空: #{“name?:’other’”}
三、#{}和#{}组合使用
#{} 和 ** $ {} ** 可以混合使用,但是必须 #{}外面 注意不能反过来
例如:
@Component
public class ERPTrackerStatusJob extends QuartzJobBean {
// 1天-->对应的分钟数
private static final long ONE_DAYS_MIN = 60 * 24L;
@Value("#{T(java.lang.Integer).parseInt('${spring.quartz.trackerStatusJob.receiveDaysNum:10}')}")
private Integer receiveDaysNum;
@Override
@Cacheable(cacheNames = "RedisCache_7D", key = "'PRODUCT-VIDEO-' + #productId+'-'+#type")
public List<ProductVideo> findByProductIdAndType(Long productId, Integer type) {
return videoRepository.findAllByProductIdAndType(productId, type);
}
}