[英]Spring: Programmatically use PropertyPlaceHolderConfigurer on none Singelton Beans
我知道PropertyPlaceHolderConfigurer的以下實現是可能的:
public class SpringStart {
public static void main(String[] args) throws Exception {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
Properties properties = new Properties();
properties.setProperty("first.prop", "first value");
properties.setProperty("second.prop", "second value");
configurer.setProperties(properties);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
context.addBeanFactoryPostProcessor(configurer);
context.setConfigLocation("spring-config.xml");
context.refresh();
TestClass testClass = (TestClass)context.getBean("testBean");
System.out.println(testClass.getFirst());
System.out.println(testClass.getSecond());
}}
在配置文件中有這個:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd “>
<bean id="testBean" class="com.spring.ioc.TestClass">
<property name="first" value="${first.prop}"/>
<property name="second" value="${second.prop}"/>
</bean>
但是,這使我看到對testBean所做的更改將顯示在所有測試bean上。
如何以這樣的方式使用propertyPlaceHolderCongfigurer,我可以將它應用於bean的各個實例,並且可以訪問這些實例中的每一個?
我希望這個問題有道理。 任何幫助將非常感激。
默認情況下,Spring bean是單例,即后續對context.getBean("testBean")
調用將返回相同的實例。 如果希望它們返回不同的實例,則應在bean定義上設置scope = "prototype"
:
<bean id="testBean" class="com.spring.ioc.TestClass" scope = "prototype">
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.