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

Vit*_*lii 1 environment-variables localtime spring-boot

我的环境变量是一个带有时间的字符串23:30在我的String Boot应用程序中,我想自动解析它并将结果设置为变量.

我试过这样的

@Value("#{ java.time.LocalTime.parse('${NIGHT_START_TIME}')}")
private LocalTime NIGHT_START_TIME;
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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

@Value("${NIGHT_START_TIME}")
private String NIGHT_START_TIME;
Run Code Online (Sandbox Code Playgroud)

任何想法如何使其工作?

pvp*_*ran 5

试试这个

 @Value("#{ T(java.time.LocalTime).parse('${NIGHT_START_TIME}')}")
 private LocalTime NIGHT_START_TIME;
Run Code Online (Sandbox Code Playgroud)

您引用类的方式是使用T.

查看文档.