我目前在spring中像这样加载我的属性文件
<context:property-placeholder location="test-esb-project-config.properties"/>
<context:property-placeholder location="esb-project-config.properties"/>对于在xml文件中使用的属性,这似乎非常有效。但是,现在如何从java代码中加载属性呢?或者我如何注入某种Bundle或Config对象,这样我就不必在一个bean中注入10个属性?
谢谢你,迪恩
发布于 2012-03-23 01:24:06
使用注解@Value(${ setter...way })效果要好得多,它将属性注入到我的bean中,而不需要输入和添加xml类型和添加属性。
发布于 2012-03-17 04:50:28
您可以为每个属性设置setter,并将它们与属性引用连接起来。
public class MyBean{
public void setFoo(String foo){ /* etc */}
public void setBar(String bar){ /* etc */}
}
<bean class="foo.bar.MyBean">
<property name="foo" value="${my.properties.foo}" />
<property name="bar" value="${my.properties.bar}" />
</bean>或者您可以将Properties对象注入到Spring Bean中。
public class MyBean{
public void setProperties(Properties properties){
// init your properties here
}
}
<bean class="foo.bar.MyBean">
<property name="properties" value="classpath:/path.to.properties" />
</bean>在使用@Value注释时,这两种方法在没有XML的情况下也可以使用。
(参见Expression Language > Annotation-based Configuration)
https://stackoverflow.com/questions/9744304
复制相似问题