Spring: Programmatically use PropertyPlaceHolderConfigurer on no Singelton Beans

I know that the following implementation of PropertyPlaceHolderConfigurer is possible:

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()); }} 

With this in the configuration file:

 <?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> 

However, it seems to me that the changes made to testBean will be shown on all beans tests.

How to use propertyPlaceHolderCongfigurer so that I can apply it to individual bean instances and have access to each of these instances?

Hope this question makes sense. Any help would be greatly appreciated.

+6
java spring properties
source share
1 answer

By default, Spring beans are single-point, that is, subsequent calls to context.getBean("testBean") return the same instance. If you want them to return different instances, you must set scope = "prototype" in the bean definition:

 <bean id="testBean" class="com.spring.ioc.TestClass" scope = "prototype"> ... 
+2
source share

All Articles