Change beans.xml filename in Spring MVC

Spring Framework logo

If you start using Spring MVC to develop Web Applications and you declare the DispatcherServlet in web.xml, it’s a little bit annoying to rename the Spring beans file to xxxx-servlet.xml, for example, if you declare DispatcherServlet like this:

Your beans.xml file has to be renamed to springservlet-servlet.xml to let DispatcherServlet find the configuration file. But, we can change this behavior by specifying to DispatcherServlet the name of our Spring Beans file in the next way:

We have to specify the file name as an init parameter:

So, at the end it will be like this:

Also, if you declare ContextLoaderListener and you aren’t using DispatcherServlet you can specify contextConfigLocation as a context-param instead init-param, like this:

And, if you organize your beans declarations, database connections, resources and so on in different configuration files, you can specify various files separated by commas (either in param-value or init-value):