I noticed that if you define a bean with same id in two xml files, it would be overiden in the second file.
Say in file a.xml i have
You can disable the feature to disallow beanoverriding by calling the setAllowBeanDefinitionOverriding
and pass false
. This has to be done early on before anything is loaded. You would either need to create your own custom ContextLoader
for this or (if you are on Spring 3.1 or up) you can create an ApplicationContextInitializer
and register this in your web.xml.
public class OverrideDisablingApplicationContextInitializer implements ApplicationContextInitializer {
public void void initialize(<? extends ConfigurableApplicationContext> applicationContext);
if (applicationContext instanceof AbstractRefreshableApplicationContext) {
(AbstractRefreshableApplicationContext (applicationContext)).setAllowBeanDefinitionOverriding(false);
}
}
in your web.xml add the following (for the ContextLoaderListener
use an init-param for the DispatcherServlet
when needed)
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>your.package.here.OverrideDisablingApplicationContextInitializer<param-value>
</context-param>
From the top of my head this should disable the overriding behavior. If you use springs WebApplicationInitializer it is even easier as you are probably constructing the ApplicationContext
yourself, you can then simply call the method directly and no ApplicationContextInitializer
is needed.
Links
Also:
final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.setAllowBeanDefinitionOverriding(false);
ctx.setConfigLocations(shardContextImport);
ctx.setParent(refreshedEvent.getApplicationContext());
ctx.refresh();