Tuesday 17 September 2013

Spring web.xml with Listener,Without LIstener

Using Two configuration files with Listener

  • Eventhough we provided the name of applicationContext.xml,It will search for spring-servlet.xml,Here servlet-name is spring

  • ContextLoaderListener,it will get the configuration file(using contextConfigLocation) and create Spring contatiner object when the project is deployed into the server

  • Dispatcher servlet creating container object with another configuration file(xxx-servlet.xml)

  • Here xxx means spring(<servlet-name>spring</servlet-name>)


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>



    Using only one configuration file with Listener

  • Here Listener and DispatcherServlet both using spring-servlet.xml


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>


    Using One Configuration file without Listener

  • We can use init parameter also for giving configuration file name,this init-param is under DispatcherServlet

  • we no need to follow particular name(xxx-servlet.xml),we no need to place that configuration file in WEB-INF folder also

  • but in this way Spring Container object is created when the DispatcherServlet object is created


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/Config/Congig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>