spring - constructor injection and overriding parent definition of nested bean

Posted by mdma on Stack Overflow See other posts from Stack Overflow or by mdma
Published on 2010-05-09T22:21:27Z Indexed on 2010/05/09 22:28 UTC
Read the original article Hit count: 310

I've read the Spring 3 reference on inheriting bean definitions, but I'm confused about what is possible and not possible.

For example, a bean that takes a collaborator bean, configured with the value 12

<bean name="beanService12" class="SomeSevice">
    <constructor-arg index="0">
       <bean name="beanBaseNested" class="SomeCollaborator">
           <constructor-arg index="0" value="12"/> 
       </bean>
    </constructor-arg>
</bean>

I'd then like to be able to create similar beans, with slightly different configured collaborators. Can I do something like

   <bean name="beanService13" parent="beanService12">
       <constructor-arg index="0">
          <bean>
             <constructor-arg index="0" value="13"/>
          </bean>
       </constructor>
   </bean>

I'm not sure this is possible and, if it were, it feels a bit clunky. Is there a nicer way to override small parts of a large nested bean definition? It seems the child bean has to know quite a lot about the parent, e.g. constructor index.

I'd prefer not to change the structure - the parent beans use collaborators to perform their function, but I can add properties and use property injection if that helps.

This is a repeated pattern, would creating a custom schema help?

Thanks for any advice!

© Stack Overflow or respective owner

Related posts about spring

Related posts about dependency-injection