Spring 如何在不注入或注入束或其他东西的情况下加载弹簧属性?
我现在像在春天一样加载我的属性文件Spring 如何在不注入或注入束或其他东西的情况下加载弹簧属性?,spring,Spring,我现在像在春天一样加载我的属性文件 <context:property-placeholder location="test-esb-project-config.properties"/> <context:property-placeholder location="esb-project-config.properties"/> 这对于在xml文件中使用的属性来说似乎非常有用。现在如何从java代码内部加载属性?或者我如何注入某种Bundle或Config对象
<context:property-placeholder location="test-esb-project-config.properties"/>
<context:property-placeholder location="esb-project-config.properties"/>
这对于在xml文件中使用的属性来说似乎非常有用。现在如何从java代码内部加载属性?或者我如何注入某种Bundle或Config对象,这样就不必在一个bean中注入10个属性
谢谢,
Dean您可以为每个属性设置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>
公共类MyBean{
公共void setFoo(字符串foo){/*etc*/}
公共无效立根杆(字符串条){/*etc*/}
}
或者,您可以将Properties对象注入到Springbean中
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>
公共类MyBean{
公共void集合属性(属性){
//在这里初始化你的属性
}
}
在使用@Value
注释时,这两种方法也可以在没有XML的情况下工作。(请参见)您可以为每个属性设置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>
公共类MyBean{
公共void setFoo(字符串foo){/*etc*/}
公共无效立根杆(字符串条){/*etc*/}
}
或者,您可以将Properties对象注入到Springbean中
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>
公共类MyBean{
公共void集合属性(属性){
//在这里初始化你的属性
}
}
在使用@Value
注释时,这两种方法也可以在没有XML的情况下工作。(请参阅)使用annotations@Value(${property})工作得更好,它将属性注入到我的bean中,而无需所有的xml键入和添加setter的工作……这方面的工作太多了。使用annotations@Value(${property})工作得更好,它将属性注入到我的bean中,而无需所有xml键入和添加setter的工作……这方面的工作太多了。这里是指这个问题吗?这里是指这个问题吗?