解析@Value属性中的LocalTime字符串

时间:2018-03-27 13:53:40

标签: spring-boot environment-variables localtime

我的环境变量是一个时间为@Value("#{ java.time.LocalTime.parse('${NIGHT_START_TIME}')}") private LocalTime NIGHT_START_TIME; 的字符串在我的String Boot应用程序中,我想自动解析它并将结果设置为变量。

我试过这个

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)

IntelliJ显示错误

  

无法解析变量'java'

这是日志

@Value("${NIGHT_START_TIME}")
private String NIGHT_START_TIME;

如果我读这样的变量,我看到该值是正确的

class Derived: Base 
{
    public override string a => "Hello";
}

任何想法如何使其发挥作用?

2 个答案:

答案 0 :(得分:5)

试试这个

 @Value("#{ T(java.time.LocalTime).parse('${NIGHT_START_TIME}')}")
 private LocalTime NIGHT_START_TIME;

您引用类的方式是使用T

检查documentation

答案 1 :(得分:0)

已在Spring Boot 2.2.6.RELEASE中进行了检查:

application.yml

NIGHT_START_TIME: '23:30'

服务

@Value("${NIGHT_START_TIME}")
private LocalTime NIGHT_START_TIME;

完美的工作而没有任何“膝盖弯曲”:)