开发者

Using @Value Spring Annotation with Groovy

开发者 https://www.devze.com 2023-02-25 10:13 出处:网络
I have a groovy class where I wan to autowire a property value. Eg: public @Value(\"${valueA}\" ) String valueA;

I have a groovy class where I wan to autowire a property value.

Eg:

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" />

The app.properties has a value set for "valueA" so in theory this should populate the String valueA in my class at runtime.

This setup works perfectly if I use a java class but not if I use a groovy class.

I get a compile error:

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

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 autowir开发者_开发百科ing the @Value parameter at runtime.


Use single quotes, ie.

public @Value('${valueA}') String valueA


since using a $ causes Groovy to interpret the annotation argument as a GString, you get a compile error. you can either escape \$ or use single quotes.

0

精彩评论

暂无评论...
验证码 换一张
取 消