Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Change Spring Web Context Configuration Filename

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 783
    Comment on it

    Change Spring Web Context Configuration Filename

    We know that if we want to implement the Spring MVC in our project then we should add DispatcherServlet into our web application deployment descriptor (web.xml) file like as below

    <servlet>
        <servlet-name>demo</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    

    Also we write a spring web configuration xxxx-servlet.xml which contains all the MVC mappings and data. By default the name of file must be XXX-servlet.xml where XXX is the name of your servlet (eg demo).

    Here one more thing, if you want to load your own created configuration file instead of demo-servlet.xml, this can be achieved by passing one init-parameter to spring dispatcher servlet as below

    <servlet>
        <servlet-name>demo</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/bean.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>
    

    Note that in above code snippet, we have passed an init-param to DispatcherServlet called contextConfigLocation. Using this parameter not only can we change the name of Springs web context file but also change its location.

    This parameter will call setContextConfigLocation method on DispatcherServlet and overrides default context config file. This will handle by Spring, no need to do any changes in user side.

    Note that it is possible to add multiple locations separated by any number of commas and spaced.

    <init-param>
            <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/bean.xml, /WEB-INF/bean-service.xml, /WEB-INF/bean-dao.xml</param-value>
        </init-param>
    

    The contextConfigLocation whenever you need to specify custom config files. This way you will be specifying both the config file names and their locations.

    The namespace is essentially an alternative way of telling Spring container context loader class what config file to use. Just use contextConfigLocation whenever you need to configure custom config files. Here is an example

    In the web.xml >>

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                http://java.sun.com/xml/ns/javaee/web-app&#95;3_0.xsd">
    
        <display-name>Spring Web Application example</display-name>
    
        <!-- Configurations for the root application context (parent context) -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:config/applicationContext.xml
                classpath:config/applicationContext-security.xml
                classpath:config/applicationContext-service.xml
                classpath:config/applicationContext-socialplugins.xml
                classpath:config/applicationContext-basicauthentication.xml 
                /WEB-INF/demo-security.xml  
            </param-value>
       </context-param>
    
    
        <!-- Configurations for the DispatcherServlet application context (child context) -->
        <servlet>
            <servlet-name>demo</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/spring/mvc/demo-servlet.xml
                </param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>demo</servlet-name>
            <url-pattern>/app/*</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: