R – How to acess a property of a bean for reading in a spring xml config file


I want to do something like the following in spring:

    <bean id="bean1" ... />
    <bean id="bean2">
        <property name="propName" value="bean1.foo" />

I would think that this would access the getFoo() method of bean1 and call the setPropName() method of bean2, but this doesn't seem to work.

Best Solution

What I understood:

  1. You have a bean (bean1) with a property called "foo"
  2. You have another bean (bean2) with a property named "propName", wich also has to have the same "foo" that in bean1.

why not doing this:

<bean id="foo" class="foopackage.foo"/>
<bean id="bean1" class="foopackage.bean1">
  <property name="foo" ref="foo"/>
<bean id="bean2" class="foopackage.bean2">
  <property name="propName" ref="foo"/>

Doing this, your bean2 is not coupled to bean1 like in your example. You can change bean1 and bean2 without affecting each other.

If you REALLY need to do the injection you proposed, you can use:

<util:property-path id="propName" path="bean1.foo"/>