Results 1 to 4 of 4

Thread: Passing parameter value from one bean to another

  1. #1
    Join Date
    Apr 2006
    Posts
    9

    Question Passing parameter value from one bean to another

    I've the following beans:

    Code:
    	<bean 
    		id="pippoBean"
    		class="test.Pippo">
    		
    		<property name="codice" value="ABCD">
    		<property name="pluto" ref="plutoBean">
    		
    	</bean>
    	
    	<bean 
    		id="plutoBean"
    		class="test.Pluto">
    		
    		<property name="codice">
    			<bean 	id="pippoBean.codice" 
    				class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
    		</property>
    		
    	</bean>
    The above doesn't work because:
    - pippoBean has a reference to plutoBean
    - plutoBean requires a property from pippoBean during initialization

    Is there an "elegant way" to pass the value of "codice" property from pippoBean to plutoBean.

    Thank you in advance

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    The beans are dependent one each other so there the solution depends on each case. In most cases the logic is distributed across the two beans and it could be simply put in another bean. Another solution is to initialize the property inside your plutoBean (you have the reference to pippioBean and just call getCodice).
    One solution a bit more complex is to add a postProcessor that will do the initialization after all the beans have been initialized.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb Use a 3rd bean?

    Could you have a third bean (with no dependencies on the other two) whose only purpose is to store the value of the "codice" property? Then the other two beans could refer to it and you wouldn't be duplicating the actual value anywhere. For example:

    Code:
    <bean id="codice" class="java.lang.String">
      <constructor-arg type="java.lang.String" value="ABCD"/>
    </bean>
    
    <bean id="pippoBean" class="test.Pippo">
      <property name="codice">
        <bean id="codice.toString"
          class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
      </property>
      <property name="pluto" ref="plutoBean">
    </bean>
        
    <bean id="plutoBean" class="test.Pluto">
      <property name="codice">
        <bean id="codice.toString"
          class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
      </property>
    </bean>
    I haven't tested this. Or possibly a better idea is to read the "codice" value from a property file?

    Andrew
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  4. #4
    Join Date
    Apr 2006
    Posts
    9

    Default

    Unfortunately the above does not work, because the bean

    Code:
    <property name="codice">
      <bean 
        id="codice.toString"
        class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
    </property>
    would cause the PropertyPathFactoryBean to call a getter for the property "toString", that is "getToString", which does not exists in java.lang.toString.

    I've found a nice-enough solution using the PropertyPlaceholderConfigurer, and setting the value of the shared property in an external properties file:

    Code:
    <bean
      id="PropertyRegistry"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="somewhere/in/classpath/registry.properties"/>
    </bean>
    
    <bean 
      id="pippoBean"
      class="test.Pippo">
      
      <property name="codice">
        <value>${prop.codice}</value>
      </property>
      <property name="pluto" ref="plutoBean">
    </bean>
      
    <bean 
      id="plutoBean"
      class="test.Pluto">
      
      <property name="codice">
        <value>${prop.codice}</value>
      </property>
    </bean>
    where prop.codice is defined in "somewhere/in/classpath/registry.properties".

    This works for me, because it allows me to share properties' values among beans.
    Any comment is welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •