Disable overriding of Spring beans in web applications

I came across this issue when I was working on a client's project which utilizes Spring for wiring dependencies. A new module in the project used the same web services as another one, so it was pretty easy for my junior co-workers to assign already used component names, thus introducing system-wide heavy failures.Although the usage of prefixes theoretically solves the issue, in practice there is no protection for overriding beans if you use a Spring container with the default setting.
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:
It would be much better and natural to support a servlet context parameter (such as contextConfigLocation) for setting this essential behavior. 

1 comment: