Search code examples
javaxmlspringspring-mvcapplicationcontext

what is the reason behind adding applicationContext-dao.xml and applicationContext-service.xml in application context


I got below code to set applicationContext-dao.xml and applicationContext-service.xml in application context.

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

     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/applicationContext-dao.xml
            /WEB-INF/config/applicationContext-service.xml
        </param-value>
    </context-param> 

Question: why to keep /WEB-INF/config/applicationContext-dao.xml and /WEB-INF/config/applicationContext-service.xml in applicationContext and -servlet.xml (proj-servlet.xml in this case) in webApplicationContext. can't we keep all the files in webApplicationContext.xml?


Solution

  • In general it's just a matter of tidiness: configuration files can grow a lot (thousands of lines) in real big applications.

    Also this approach allows you to divide in modules a big application without the pain of splitting this file. I've worked in several projects where the database access layer has a SOAP/REST API and more than one front-end applications obtain any needed data through that API, so this service is deployed as a standalone module.