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

StackOverflow https://stackoverflow.com/questions/22358241

Question

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?

Was it helpful?

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.

OTHER TIPS

You can join

/WEB-INF/config/applicationContext-dao.xml /WEB-INF/config/applicationContext-service.xml

files. No problem with that. But its good to keep different concepts on different files, for readibility and maintainability reasons.

But always keep your web staff in a different file for webapplicationcontext.

Yes, you could keep them in the same file, but for larger projects it can be easier to maintain if you separate them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top