使用@Value春天注释使用Groovy [英] Using @Value Spring Annotation with Groovy

查看:1295
本文介绍了使用@Value春天注释使用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有值a设置,因此在理论上这应该在我的在运行时类填充字符串值a的值。

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.

我得到一个编译错误:

错误:预期'$值a'是java.lang.String类型的内联常数@ org.springframework.beans.factory.annotation.Value结果
  错误:属性'值'的类型应该是'java.lang.String中';但发现型的java.lang.Object在@ org.springframework.beans.factory.annotation.Value

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

这篇关于使用@Value春天注释使用Groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆