When importing the a.xml:
<bean id="a" class="A"/>
<bean id="b" class="B">
<constructor-arg ref="a"/>
</bean>
This is ugly to an init method on a client class to modify the bean a
.
I don't really undestand the problem here: some framework is probably providing you an already prepared, ready to run, spring context. If you don't like this already prepared stuff, why do you want to modify it at runtime with fancy strategies? It is just a context.
You can reuse the beans you want in that context, and if you don't like the bean a
, just declare your own bean a2
.
This is only a problem when the bean a
is used in many beans of the context you import.
If this is the case, then you can copy the a.xml context and customize it.
If you use a namespace because it is easier, sometimes you find out that some options are not possibles when using the namespace, and you finally go with using a FactoryBean or declaring manually the beans. When importing an external xml context it is the same: sometimes it doesn't do what you want.
Spring helps you wire objets together. A namespace or external context is a predefined wiring. If you don't like the predefined wiring, just do it yourself with your own xml or FactoryBean.
If the wiring to copy and customize is quite large (multiple xml files etc...) and you just want to perform a little modification on a single bean, then you should give it a try to what Thomas Jung tells you: override the bean you don't like. It works!
Spring's overriding bean