Announcement Announcement Module
Collapse
No announcement yet.
How to fill bean with properties if it can not be done directly? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to fill bean with properties if it can not be done directly?

    I've got some legacy code that I have to work with in Spring context.
    There is some container class (let's call it target bean) that has some internal properties that can not be injected neither by constructor nor by setters. But there are also special classes (let's call them bean configurers) that have the right to fill properties of the target bean. All I have to do (assuming I'm doing it programmatically) is to call a set of fill methods on bean configurers passing target bean and properties as method arguments, like this:
    Code:
    TargetClass target = new TargetClass();
    ConfigurerClass1 configurer1 = new ConfigurerClass1();
    ConfigurerClass2 configurer2 = new ConfigurerClass2();
    configurer1.fillProp1(target, "value");
    configurer1.fillProp2(target, new SomeClass());
    configurer2.fillProp3(target, new int[]{1,2});
    configurer2.fillProp4(target, true);
    Sorry for obfuscated code, I can not post original code.

    My question is what is the best way to inject all those properties and get target bean configured in Spring context.

    Thank you.

  • #2
    I'm looking at MethodInvokingFactoryBean. It looks like what I need.
    Will write back the result.

    Comment


    • #3
      I've found a solution. Somewhat verbose, but it works:
      Code:
      <bean id="targetUser"
                 depends-on="prop1Filler,prop2Filler,prop3Filler,prop4Filler" ...>
          <property name="target" ref="target">
        </bean>
      
        <bean id="target" class="TargetClass"/>
      
        <bean id="configurer.invoker.parent" abstract="true"
       class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
         <property name="arguments">
           <list>
             <ref bean="target"/>
           </list>
         </property>
        </bean>
        
        <bean id="configurer1.invoker.parent" abstract="true"
                 parent="configurer.invoker.parent">
          <property name="targetObject">
            <bean class="ConfigurerClass1"/>
          </property>
        </bean>
      
        <bean id="configurer2.invoker.parent" abstract="true"
                 parent="configurer.invoker.parent">
          <property name="targetObject">
            <bean class="ConfigurerClass2"/>
          </property>
        </bean>
      
        <bean id="prop1Filler" parent="configurer1.invoker.parent">
          <property name="targetMethod" value="fillProp1"/>
          <property name="arguments">
            <list merge="true">
              <value>value</value>
            </list>
          </property>
        </bean>
      
        <bean id="prop2Filler" parent="configurer1.invoker.parent">
          <property name="targetMethod" value="fillProp2"/>
          <property name="arguments">
            <list merge="true">
              <value><bean class="SomeClass"/></value>
            </list>
          </property>
        </bean>
        
        <bean id="prop3Filler" parent="configurer2.invoker.parent">
          <property name="targetMethod" value="fillProp3"/>
          <property name="arguments">
            <list merge="true">
              <value>
                 <list>
                    <value>1</value>
                    <value>2</value>
                 </list>
              </value>
            </list>
          </property>
        </bean>
      
        <bean id="prop4Filler" parent="configurer2.invoker.parent">
          <property name="targetMethod" value="fillProp4"/>
          <property name="arguments">
            <list merge="true">
              <value>true</value>
            </list>
          </property>
        </bean>
      This might be a good example of not-that-easy XML-configuration that works.

      Comment


      • #4
        Ok I got lost somewhere in there, whatever it was you were doing. This might be useful however to cut down the XML.
        http://www.springframework.org/docs/...sible-xml.html
        Last edited by karldmoore; Aug 29th, 2007, 01:19 PM.

        Comment

        Working...
        X