Spring 3.0.5 不评估属性中的 @Value 注释 [英] Spring 3.0.5 doesn't evaluate @Value annotation from properties
问题描述
尝试将属性自动连接到 Spring 3.0.5.RELEASE 中的 bean,我正在使用:
Trying to auto-wire properties to a bean in Spring 3.0.5.RELEASE, I'm using:
config.properties
:
username=myusername
main-components.xml
:
<context:property-placeholder location="classpath:config.properties" />
我的班级:
MyClass:
@Service
public class MyClass {
@Value("${username}")
private String username;
...
}
因此,用户名被设置为 literally "${username}"
,因此表达式不会被解析.我对此类的其他自动关联依赖项已设置,并且 Spring 不会引发任何异常.我也尝试添加 @Autowired
但没有帮助.
As a result, username gets set to literally "${username}"
, so the expression doesn't get parsed. My other auto-wired dependencies on this class get set, and Spring doesn't throw any exception. I also tried to add @Autowired
but it didn't help.
如果我将属性解析为单独的 bean,然后使用 @Autowired
+ @Qualifier
,它可以工作:
If I parse properties to a separate bean and then use @Autowired
+ @Qualifier
, it works:
<bean id="username" class="java.lang.String">
<constructor-arg value="${username}"/>
</bean>
任何想法如何使用 @Value
?也许我需要包含一些我没有的 Spring 依赖项?谢谢
Any ideas how to use just @Value
? Maybe I need to include some Spring dependency that I haven't? Thank you
推荐答案
找到问题所在.从评论中复制/粘贴:
Found what the issue was. Copy/paste from comments:
您确定 <context:property-placeholder>
与 MyClass bean 在同一个应用程序上下文中(而不是在父上下文中)吗?– axtavt
Are you sure you have <context:property-placeholder>
in the same application context as your MyClass bean (not in the parent context)? – axtavt
你是对的.我将 <context:property-placeholder>
从 ContextLoaderListener
定义的上下文移动到 servlet 上下文.现在我的值被解析了.非常感谢!- 亚历克斯
You're right. I moved <context:property-placeholder>
from the context defined by the ContextLoaderListener
to the servlet context. Now my values get parsed. Thanks a lot! - alex
这篇关于Spring 3.0.5 不评估属性中的 @Value 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!