Spring: Programmatically use PropertyPlaceHolderConfigurer on none Singelton Beans

I'm aware that the following implementation of a 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 config 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, this looks to me that the changes made to the testBean will be shown on all the test beans.

How do I use the propertyPlaceHolderCongfigurer in such a way that I can apply it to individual instances of the bean, and have access to each of these instances?

I hope the question makes sense. Any help would be much appreciated.

Answers


By default Spring beans are singletons, that is subsequent calls to context.getBean("testBean") would return the same instance. If you want them to return different instances, you should set a scope = "prototype" on the bean definition:

<bean id="testBean" class="com.spring.ioc.TestClass" scope = "prototype"> 
...

Need Your Help

percona replication change master

mysql percona

I'm trying to set up replication between two MySQL servers. From the slave I log in as follows:

Recommendations for sites / articles / books on developing web sites in Ruby without using a framework such as Rails / Merb

ruby

Im struggling to find good material about developing web applications in Ruby without using a framework such as Rails or Merb in the usual places (I've already spent a while on Google, Safari books

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.