I have a web application that use Sring IoC framework.
I use the Java configuration for Spring, and I only use @Configuration annoted module definition (no DI related tags elsewhere in the code).
The Spring registry is built on web application start-up thanks to (a bit modified version of) Spring context loader listener, and the contextConfigLocation param in web.xml configured to point to the @Configuration annotated class.
All that is good and I get a AnnotationConfigWebApplicationContext.
Now, I want to have plugins in my application, that will have their own @Configuration annotated configuration classes, and will use some of the main application services. BUT I don’t want to have main application to be modified to load these new modules.
So, I thought that I could simply use the package searching of annotated class for that, but now, it seems that I can use two beans with the same type, even if they have different ids, and clearly AnnotationConfigWebApplicationContext doc states that:
Note: In case of multiple @Configuration classes, later
@Beandefinitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra Configuration class.
I don’t want that, because modules should be able to contribute alternative version of services, not (alwways) override existing one – especcially if I want to have a “moduleDef” bean.
I tried to use differents approach on that, but the hierachy of Context and related services is just to big for me.
So, does anybody know how I could reach my goal ?
Thanks
You can have multiple beans of the same type, but You cannot have 2 or more beans with the same ID in a single Spring ApplicationContext – no matter if You use XML or JavaConfig.
The overriding mechanism matches the bean ID’s, so all You need to do is to ensure unique ID, i.e.:
coreModuleDef,someOtherModuleDef,anotherModuleDef. I don’t think You need the ID of each module definition to be identical? What should be sufficient is the type to be the same, but not ID.You can also turn off the overriding mechanism by setting
allowBeanDefinitionOverridingtofalseon YourAnnotationConfigWebApplicationContextto get an exception if You accidentally override a bean:or: