Announcement Announcement Module
Collapse
No announcement yet.
Dynamically changing the props of PropertyPlaceholderConfigurer in context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically changing the props of PropertyPlaceholderConfigurer in context

    I actually need to create beans out of the config file.My problem is I need to specify some of the props as variables

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    	
    	<bean id="testBean" class="com.TestBeanProps" lazy-init="true" scope="prototype">
    		<property name="name">
    			<value>${name}</value>
    		</property>
    	</bean>
    </beans>
    Please note that the scope of this bean is prototype, so I will want to create multiple instances of this bean, each with a different property "name".

    The way I am doing it currently is

    Code:
    	
    	public static void main(String[] args) {
    		XmlBeanFactory ctx = new XmlBeanFactory(new FileSystemResource("asyncDynamicConsumer.xml"));
    		XmlBeanFactory ctx2 = new XmlBeanFactory(new FileSystemResource("asyncDynamicConsumer.xml"));
    		
    		if(ctx == ctx2 || ctx.equals(ctx2)){
    			System.out.println("Both contexes are same");
    		}else{
    			System.out.println("Both contexes are diff");
    		}
    		
    		PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    		Properties props = new Properties();
    		props.setProperty("name", "MyName");
    		props.setProperty("bla", "blabla");
    		cfg.setProperties(props);
    		cfg.postProcessBeanFactory(ctx);
    		
    		TestBeanProps propers = (TestBeanProps) ctx.getBean("testBean");
    		System.out.println(propers.getName());
    	
    		PropertyPlaceholderConfigurer cfg2 = new PropertyPlaceholderConfigurer();
    		Properties props2 = new Properties();
    		props2.setProperty("name", "YourName");
    		props2.setProperty("bla", "blabla");
    		cfg2.setProperties(props2);
    		cfg2.postProcessBeanFactory(ctx2);
    		TestBeanProps propers3 = (TestBeanProps) ctx2.getBean("testBean");
    		System.out.println(propers3.getName());
    		
    	}
    The thing about this code is I am creating a new context every time I need to create a new bean. This is not desired. Is it possible to use the same context over and over to create beans out of it?

    An alternate approach is
    Code:
    ClassPathApplicationContext ctx = new ClassPathApplicationContext("asyncDynamicConsumer.xml");
    
    		TestBeanProps propers = (TestBeanProps) ctx.getBean("testBean");
    propers.setName("MyName");
    
    
    		TestBeanProps propers2 = (TestBeanProps) ctx.getBean("testBean");
    propers2.setName("YourName");
    But I don't think this is springs friendly approach.

    Please help.

  • #2
    Perhaps I'm misinterpreting what you're trying to do, but this might be much simpler than you're making it.

    If you need to create a prototype-scope bean and control some of the property values for that bean at the time you create it, is there some reason why you can't just, like, do that?

    I mean, perhaps you don't need to use Spring for this? It sounds like you need direct control of the creation of this object at the time of creation, so just create a new instance of the class, set the desired properties, and send it to the setter or constructor that needs it.

    Comment


    • #3
      agreed, but

      Originally posted by dkarr View Post
      Perhaps I'm misinterpreting what you're trying to do, but this might be much simpler than you're making it.

      If you need to create a prototype-scope bean and control some of the property values for that bean at the time you create it, is there some reason why you can't just, like, do that?

      I mean, perhaps you don't need to use Spring for this? It sounds like you need direct control of the creation of this object at the time of creation, so just create a new instance of the class, set the desired properties, and send it to the setter or constructor that needs it.
      Yup you are rite.. that is wat m actually doing in the second approach. Why springs, I am actually using Spring for JMS where in springs provide me DefaultMEssageListenerContainers, helps me to pool better and all that..

      So that is the reason why springs.. I agree I can still create objects of these classes and just use them as some third party lib, just not sure if that is the rite idea, coz then i'll have to make sure i handle all the calls like intialize(), shutdown() etc

      Also is there ne thing wrong with the first approach, as in creating multiple contexts.

      Comment

      Working...
      X