Note that there are some explanatory texts on larger screens.

plurals
  1. POspring - constructor injection and overriding parent definition of nested bean
    text
    copied!<p>I've read the Spring 3 reference on <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-child-bean-definitions" rel="nofollow noreferrer">inheriting bean definitions</a>, but I'm confused about what is possible and not possible.</p> <p>For example, a bean that takes a collaborator bean, configured with the value 12</p> <pre><code>&lt;bean name="beanService12" class="SomeSevice"&gt; &lt;constructor-arg index="0" ref="serviceCollaborator1"/&gt; &lt;/bean&gt; &lt;bean name="serviceCollaborator1" class="SomeCollaborator"&gt; &lt;constructor-arg index="0" value="12"/&gt; &lt;!-- more cargs, more beans, more flavor --&gt; &lt;/bean&gt; </code></pre> <p>I'd then like to be able to create similar beans, with slightly different configured collaborators. Can I do something like</p> <pre><code> &lt;bean name="beanService13" parent="beanService12"&gt; &lt;constructor-arg index="0"&gt; &lt;bean&gt; &lt;constructor-arg index="0" value="13"/&gt; &lt;/bean&gt; &lt;/constructor&gt; &lt;/bean&gt; </code></pre> <p>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.</p> <p>This is a toy example - in practice the service is a large bean definition relying on many other collaborator beans, which have also other bean dependencies. For example, a chain of handlers were created with each bean referencing the next in the chain, which references the next. I want to create an almost identical chain with some small changes to handlers in the middle, how do I it?</p> <p>I'd prefer not to change the structure - the service beans use collaborators to perform their function, but I can add properties and use property injection if that helps. </p> <p>This is a repeated pattern, would creating a custom schema help?</p> <p>Thanks for any advice!</p> <p>EDIT: The nub of my question is, if I have a really large bean definition, with a complex hiearchy of beans being created (bean having properites that are bean etc.), and I want to create a bean that is almost the same with a few changes, how to I do it? Please mention if your solution has to use properites, or if constructor injection can be used.</p> <p>Nested vs. top-level beans are not the issue (in fact, I think all the beans are top level in practice.)</p> <p>EDIT2: Thank you for your answers so far. A FactoryBean might be the answer, since that will reduce the complexity of the spring context, and allow me to specify just the differences as parameters to the factory. But, pushing a chunk of context back into code doesn't feel right. I've heard that spring can be used with scripts, e.g. groovy - does that provide an alternative? Could the factory be created in groovy?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload