在 Groovy 中使用 @Value Spring 注解 [英] Using @Value Spring Annotation with Groovy
问题描述
我有一个 groovy 类,我想在其中自动装配一个属性值.
I have a groovy class where I wan to autowire a property value.
例如:
public @Value("${valueA}" ) String valueA;
在我的应用程序上下文中添加属性占位符
With the addition of the the property-placeholder in my appliction context
<context:property-placeholder location="classpath:spring/app.properties" />
app.properties 为valueA"设置了一个值,因此理论上这应该在运行时填充我的类中的字符串 valueA.
The app.properties has a value set for "valueA" so in theory this should populate the String valueA in my class at runtime.
如果我使用 java 类,则此设置可以完美运行,但如果我使用 groovy 类,则不会.
This setup works perfectly if I use a java class but not if I use a groovy class.
我得到一个编译错误:
错误:预期 '$valueA' 是 @org.springframework.beans.factory.annotation.Value 中 java.lang.String 类型的内联常量
错误:属性值"应具有类型java.lang.String";但在@org.springframework.beans.factory.annotation.Value 中找到了类型'java.lang.Object'
Error: expected '$valueA' to be an inline constant of type java.lang.String in @org.springframework.beans.factory.annotation.Value
Error: Attribute 'value' should have type 'java.lang.String'; but found type 'java.lang.Object' in @org.springframework.beans.factory.annotation.Value
我只想知道在使用 groovy 类时上述语法是否正确,如果不正确,在运行时自动装配 @Value 参数的正确语法是什么.
I just want to know if the above syntax is correct when using a groovy class and if not what is the correct syntax for autowiring the @Value parameter at runtime.
推荐答案
使用单引号,即.
public @Value('${valueA}') String valueA
这篇关于在 Groovy 中使用 @Value Spring 注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!