java 如何使用 spring 2.5.x 将单个属性值注入字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I inject a single property value into a string using spring 2.5.x?
提问by krosenvold
I would really like to annotate a method with a reference to a single property in a property file for injection.
我真的很想用对注入的属性文件中的单个属性的引用来注释一个方法。
@Resource("${my.service.url}")
private String myServiceUrl;
Of course, this syntax does not work ;) Thats why I'm asking here.
当然,这种语法不起作用;) 这就是我在这里问的原因。
I am aware that I can inject the full properties file, but that just seems excessive, I dont want the property file - I want the configured value.
我知道我可以注入完整的属性文件,但这似乎过多,我不想要属性文件 - 我想要配置的值。
Edit: I can only see PropertyPlaceholderConfigurer examples where XML is used to wire the property to the given field. I still cannot figure out how this can be achieved with an annotation ?
编辑:我只能看到 PropertyPlaceholderConfigurer 示例,其中使用 XML 将属性连接到给定字段。我仍然无法弄清楚如何通过注释来实现这一点?
采纳答案by Dónal
There's a thread about this on the Spring forum. The short answer is that there's really no way to inject a single property using annotations.
Spring 论坛上有一个关于此的主题。简短的回答是,真的没有办法使用注解注入单个属性。
I've heard that the support for using annotations will be improved in Spring 3.0, so it's likely this will be addressed soon.
我听说 Spring 3.0 将改进对使用注解的支持,所以很可能很快就会解决这个问题。
回答by
I know it has been a while since the original post but I have managed to stumble across a solution to this for spring 2.5.x
我知道距离最初的帖子已经有一段时间了,但我设法偶然发现了 spring 2.5.x 的解决方案
You can create instances of "String" beans in the spring xml configuration which can then be injected into the Annotated components
您可以在 spring xml 配置中创建“字符串”bean 的实例,然后将其注入到带注释的组件中
@Component
public class SomeCompent{
@Autowired(required=true
@Resource("someStringBeanId")
private String aProperty;
...
}
<beans ....>
<context:component-scan base-package="..."/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
...
</bean>
<bean id="someStringId" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${place-holder}"/>
</bean>
</beans>
回答by Ricardo Gladwell
I've created a project which addresses this problem for Spring 2.5.*:
我为 Spring 2.5.* 创建了一个解决这个问题的项目:
http://code.google.com/p/spring-property-annotations/
http://code.google.com/p/spring-property-annotations/
For Spring 3 you can use the @Value("${propery.key}") annotation.
对于 Spring 3,您可以使用 @Value("${propery.key}") 注释。
回答by miceuz
you can do this if you use XML configuration. Just configure PropertyPlaceholderConfigurer and specify property value in configuration
如果您使用 XML 配置,则可以执行此操作。只需配置 PropertyPlaceholderConfigurer 并在配置中指定属性值
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/foo/jdbc.properties</value>
</property>
</bean>
<bean ...>
<property name="myServiceUrl" value="${my.service.url}"/>
</bean>
回答by Hubert
You could try injecting value of property "my.service.url" to a filed in your bean.
您可以尝试将属性“my.service.url”的值注入到 bean 中的文件中。
Take a look at: http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer
看看:http: //static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer
HTH.
哈。

