I have to load 3 spring config xml files in myproj spring mvc app named myproj-controllers.xml, myproj-services.xml and myproj-dao.xml. I have two options to load them.
Firstly Use import resources in myproj-servlet.xml
<import resource="myproj-controllers.xml"/>
<import resource="myproj-services.xml"/>
<import resource="myproj-dao.xml"/>
or secondly in the web.xml using context param like this
<context-param>
<param-name>contextConfigLocation</param-name>
<param-values>/WEB-INF/myproj-controllers.xml</param-values>
<param-values>/WEB-INF/myproj-services.xml</param-values>
<param-values>/WEB-INF/myproj-dao.xml</param-values>
</context-param>
and adding ContextLoader listener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Which approach is recommend? And why? In my opinion I find import approach easier as we only need to make changes to myproj-servlet.xml instead of web.xml.
Spring lets you declare multiple contexts in a parent-child relation so I always went for one root
applicationContext.xmlcontaining my application beans (services, DAOs etc) and oneaction-servlet.xmlfor servlet contexts (request mappings, view resolvers etc).I once needed
action-servlet-2.xmlfile but still had just one rootapplicationContext.xmlfor both servlet contexts.So (for me) it was always parent context + child context.
The only need for splitting the files into more pieces was just to reduce the size of the XMLs (which is what
<import>does best).For me, the
contextConfigLocationparam refers to application context files being loaded together into a single application context instance. But your files (myproj-controllers.xml,myproj-services.xml,myproj-dao.xml) seem like parts of one application context file.For this reason I would personally go for the
<import>statements and have just one value (for the root application context) in thecontextConfigLocationparam.