使用 spring3 @Value 访问 PropertyPlaceholderConfigurer 值?
当我的属性源是 PropertyPlaceholderConfigurer
的子类时,我尝试使用 @Value
设置 spring bean 中的字符串值。有人知道该怎么做吗?
I'm trying to set the value of a string in a spring bean using @Value
, when my property source is a subclass of PropertyPlaceholderConfigurer
. Anyone know how to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老问题,但仍然值得回答。您可以像使用原始
PropertyPlaceholderConfigurer
一样使用该表达式。app.properties
app-context.xml
目标 bean 中的
使用 Spring 3.0.6 测试了此方法
Old question, but still worth to be answered. You can use the expression the same way as you would with the original
PropertyPlaceholderConfigurer
.app.properties
app-context.xml
in the target bean
Tested this approach using Spring 3.0.6
您是否设法通过使用属性语法从 bean 定义文件中显式注入值来使其工作?理论上,如果这有效,那么您应该能够在
@Value
中使用相同的表达式。就此而言,您也应该能够使用@Autowired @Qualifier
来做到这一点Have you managed to get it to work by explicitly injecting the value from the bean definition file using the property syntax? In theory, if that works, then you should be able to use the same expression in
@Value
. For that matter, you should be able to do it using@Autowired @Qualifier
also我认为不可能在
@Value
注释中使用 SPEL 访问由PropertyPlaceHolderConfigurer
加载的属性。那就太好了,但据我所知,下一个最好的事情是声明:它可以指向与您的
PropertyPlaceHolderConfigurer
相同的属性文件。I don't think it is possible to access properties loaded by
PropertyPlaceHolderConfigurer
using SPEL in a@Value
annotation. It would be great, but as far as I know, the next best thing is to declare:It can point to the same properties file as your
PropertyPlaceHolderConfigurer
.