tags:

views:

2980

answers:

8

I have a handful of projects that all use one project for the data model. Each of these projects has its own applicationContext.xml file with a bunch of repetitive data stuff within it.

I'd like to have a modelContext.xml file and another for my ui.xml, etc.

Can I do this?

+1  A: 

Yes, you can do this via the import element.

<import resource="services.xml"/>

Each element's resource attribute is a valid path (e.g. classpath:foo.xml)

enricopulatzo
+8  A: 

From the Spring Docs (v 2.5.5 Section 3.2.2.1.):

It can often be useful to split up container definitions into multiple XML files. One way to then load an application context which is configured from all these XML fragments is to use the application context constructor which takes multiple Resource locations. With a bean factory, a bean definition reader can be used multiple times to read definitions from each file in turn.

Generally, the Spring team prefers the above approach, since it keeps container configuration files unaware of the fact that they are being combined with others. An alternate approach is to use one or more occurrences of the element to load bean definitions from another file (or files). Let's look at a sample:

<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>

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

In this example, external bean definitions are being loaded from 3 files, services.xml, messageSource.xml, and themeSource.xml. All location paths are considered relative to the definition file doing the importing, so services.xml in this case must be in the same directory or classpath location as the file doing the importing, while messageSource.xml and themeSource.xml must be in a resources location below the location of the importing file. As you can see, a leading slash is actually ignored, but given that these are considered relative paths, it is probably better form not to use the slash at all. The contents of the files being imported must be valid XML bean definition files according to the Spring Schema or DTD, including the top level element.

Nicholas Trandem
Absolutely agree with the Spring docs: aggregation of config files beats explicit imports every time. If for nothing else than for unit testing.
Boris Terzic
+1  A: 

We do this in our projects at work, using the classpath* resource loader in Spring. For a certain app, all appcontext files containing the application id will be loaded:

classpath*:springconfig/spring-appname-*.xml
Asgeir S. Nilsen
I'm confused. new ClassPathResource("classpath*:springconfig/spring-appname-*.xml") is what you're proposing?
Allain Lalonde
+1  A: 

Given what Nicholas pointed me to I found this in the docs. It allows me to pick at runtime the bean contexts I'm interested in.

GenericApplicationContext ctx = new GenericApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions(new ClassPathResource("modelContext.xml"));
xmlReader.loadBeanDefinitions(new ClassPathResource("uiContext.xml"));
ctx.refresh();
Allain Lalonde
A: 

Here's what I've done for one of my projects. In your web.xml file, you can define the Spring bean files you want your application to use:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/applicationContext.xml
      /WEB-INF/modelContext.xml
      /WEB-INF/ui.xml
    </param-value>
  </context-param>

If this isn't defined in your web.xml, it automatically looks for /WEB-INF/applicationContext.xml

Michael Angstadt
I'm not using Spring MVC, just the IOC
Allain Lalonde
A: 

Another thing to note is that although you can do this, if you aren't a big fan of XML you can do a lot of stuff in Spring 2.5 with annotations.

bpapa
A: 

Yes, you can using the tag inside the "Master" bean file. But what about the why? Why not listing the files in the contextConfigLocation context param of the wab.xml or als locations array of the bean factory?

I think mutliple files are much easier to handle. You may choose only some of them for a test, simply add rename or remove a part of the application and you may boundle different applications with the same config files (a webapp and a commandline version with some overlapping bean definitions).

Arne Burmeister
A: 

Hi I am trying to do this import in following way :

<import resource="services.xml"/> 
<import resource="resources/messageSource.xml"/> 
<import resource="/resources/themeSource.xml"/> 

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

But some how it is considering only beans in last last configuration file. But For remaining bean ids i am getting error No mapping for XXX..

Can you please explain me why i am getting this error.

vishnu