I’ve got a bean (id=”myBean”) defined in e.g. core.xml. myBean is NOT abstract, and its definition in this file CANNOT be modified.
However I need to set a new property (myProperty) somehow, in a separate XML file (overrides.xml) that is loaded after core.xml is loaded at a particular client site.
If I redefine the bean a second time, and this time give myProperty a value, Spring (2.5) rejects it as a duplicate bean definition (fair enough).
I was wondering how to set this property in overrides.xml without breaking/changing the original bean definition.
Guessing AOP and possibly an AutoProxy is somehow the way to do achieve this but so far I can’t find an answer. If anyone can help would appreciate it.
By the way, my problem is exactly the same as How do I override a Spring bean definition yet still reference the overridden bean? but I have yet to figure out how the author fixed it in the end (and there is also a suggestion that it’s not possible, which would make me very sad if true…!)
Thanks
You can use a
BeanPostProcessorbased solution:This is the bean definition:
Now all beans whose bean names can be found in your map will be populated with the additional properties you specified.
I don’t know if this approach will work in all kinds of hierarchical context, but I verified that it works if the context that defines the Processor imports the other context.
This solution does not apply to the other question, because that was about constructor injection. This solution only works with standard java bean properties.