使用 @Value 创建一个对象

huangapple go评论58阅读模式
英文:

Create an object using @Value

问题

I'm trying to use @Value annotation to read a string parameter value and create an object using this value.
I've tried the following:

The parameter in application.yml

app:
  report:
    time-zone: +00:00

Creating the object using the parameter:

@Value("#{ java.time.ZoneId.of('app.report.time-zone') }")
ZoneId reportZoneId;

But got the error:

Unsatisfied dependency expressed through field 'reportZoneId':
Expression parsing failed

Is something wrong with the syntax or isn't it possible at all?

英文:

I'm trying to use @Value annotation to read a string parameter value and create an object using this value.
I've tried the following:

The parameter in application.yml

app:
  report:
    time-zone: +00:00

Creating the object using the parameter:

@Value("#{ java.time.ZoneId.of('app.report.time-zone') }")
ZoneId reportZoneId;

But got the error:

> Unsatisfied dependency expressed through field 'reportZoneId':
> Expression parsing failed

Is something wrong with the syntax or isn't it possible at all?

答案1

得分: 3

这是正确的语法:

@Value("#{T(java.time.ZoneId).of('${app.report.time-zone}')}")
ZoneId reportZoneId;
英文:

This is the correct syntax for the case:

@Value("#{T(java.time.ZoneId).of('${app.report.time-zone}')}")
ZoneId reportZoneId;

huangapple
  • 本文由 发表于 2023年5月22日 16:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定