我的环境变量是一个带有时间23:30
的字符串,我想自动解析它并将结果设置为变量。
我尝试过这样的尝试
@Value("#{ java.time.LocalTime.parse('${NIGHT_START_TIME}')}")
private LocalTime NIGHT_START_TIME;
Intellij显示错误
无法解析变量'java'
这是日志
Unsatisfied dependency expressed through field 'NIGHT_START_TIME';
nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'LocalTime' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
如果我读取这样的变量,我会看到该值正确
@Value("${NIGHT_START_TIME}")
private String NIGHT_START_TIME;
有什么想法使它起作用?
尝试此
@Value("#{ T(java.time.LocalTime).parse('${NIGHT_START_TIME}')}")
private LocalTime NIGHT_START_TIME;
您引用类使用T
的方式。
检查文档。
已在Spring Boot 2.2.6.Release上检查:
application.yml
NIGHT_START_TIME: '23:30'
服务
@Value("${NIGHT_START_TIME}")
private LocalTime NIGHT_START_TIME;
在没有任何"膝盖弯"的情况下工作完美:(