Saturday 11 July 2015

Spring : Configuring web.xml programatically

Spring 3.1introdced the support for Servlet 3's code-based configuration, which can be used as an alternative to the web deployment descriptor file web.xml. For this purpose, Spring has come up with 
an interface namely WebApplicationInitializer. 


The traditional web.xml file I have with me is as follows : 



And my servlet configuration file assortedmind-servlet.xml is as follows :



The servlet configuration is kept simple for the time sake. 

With these traditional xmls in mind, let us try to achieve the same purpose through a code based approach. The following piece of code can be used to replace web.xml



And the assortedmind-servlet.xml can be coded as follows :