views:

2189

answers:

1

What is the correct way to split Spring's configuration to multiple xml files? At the moment I have

  • /WEB-INF/foo-servlet.xml
  • /WEB-INF/foo-service.xml
  • /WEB-INF/foo-persistence.xml

My web.xml has the following:

<servlet>
 <description>Spring MVC Dispatcher Servlet</description>
 <servlet-name>intrafest</servlet-name>
 <servlet-class>
  org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/foo-*.xml
        </param-value>
 </init-param>
 <load-on-startup>2</load-on-startup>
</servlet>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
         /WEB-INF/foo-*.xml
 </param-value>
</context-param>


<listener>
 <listener-class>
  org.springframework.web.context.ContextLoaderListener
 </listener-class>
</listener>

The actual questions:

  • Is this approach "correct"/"best"?
  • Do I really need to specify the config locations both in the DispatcherServlet AND the context-param sections?

What do I need to keep in mind to be able to reference beans defined in foo-servlet.xml from foo-service.xml? Does this have something to do with specifying contextConfigLocation in web.xml?

Update 1:

I'm using Spring framework 3.0. I'ts my understanding that I don't need to do resource importing like

 <import resource="foo-services.xml"/>".

Is this a correct assumption?

+2  A: 

I find the following setup the easiest.

Use the default config file loading mechanism of DispatcherServlet:

The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).

In your case, simply create a file intrafest-servlet.xml in the WEB-INF dir and don't need to specify anything specific information in web.xml.

In intrafest-servlet.xml file you can use import to compose your XML configuration.

<beans>
  <bean id="bean1" class="..."/>
  <bean id="bean2" class="..."/>

  <import resource="foo-services.xml"/>
  <import resource="foo-persistence.xml"/>
</beans>

Note that the Spring team actually prefers to load multiple config files when creating the (Web)ApplicationContext. If you still want to do it this way, I think you don't need to specify both context parameters (context-param) and servlet initialization parameters (init-param). One of the two will do. You can also use commas to specify multiple config locations.

eljenso
+1 - my setup looks just like this. Although I don't think there are really any practical advantages/disadvantages to this setup vs specifying multiple config files in web.xml - it just seems like semantics
matt b
I definitely think that the default config is advantageous: convention over configuration. Instead of specifying multiple config files with *extra config*, you have only one *default* "top-level" config file that will import those same files you would otherwise have to specify anyway.
eljenso