tintin
tintin

Reputation: 5867

What is the best way to load spring config xmls?

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.

Upvotes: 1

Views: 954

Answers (2)

user159088
user159088

Reputation:

Spring lets you declare multiple contexts in a parent-child relation so I always went for one root applicationContext.xml containing my application beans (services, DAOs etc) and one action-servlet.xml for servlet contexts (request mappings, view resolvers etc).

I once needed action-servlet-2.xml file but still had just one root applicationContext.xml for 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 contextConfigLocation param 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 the contextConfigLocation param.

Upvotes: 1

duffymo
duffymo

Reputation: 308743

I prefer the context loader listener approach, but perhaps that's because I've never considered the import method. I'll try it out. Thanks.

I don't see any performance advantages. The WAR file has to be redeployed in either case. It's modifying one file as opposed to another. I don't see any difference. It has a bike shed feel to me, but I could be wrong.

Upvotes: 0

Related Questions