Changing such settings is no problem in applications where you have the control of instantiating the container, but it is cumbersome in case of web applications using Spring's ContextLoaderListener for booting the container. Namely, this ServletContextListener takes care of the creation of the appropriate ContextLoader, which in turn constructs the ConfigurableWebApplicationContext whose properties can be adjusted.
So the following steps are required to disable bean definition overriding in a web app:
- Subclass ContextLoader
- Override its customizeContext method (which will be called by createWebApplicationContext) and call setAllowBeanDefinitionOverriding(false) on the context there
- Subclass ContextLoaderListener or implement your own ServletContextListener which uses the subclassed ContextLoader
- Replace the standard by the custom ContextLoaderListener in the web application's web.xml.