tags:

views:

433

answers:

4

Hi all,

I am sure that the above question has a straightforward answer but I couldn't easily find it (neither in the documentation nor on stackoverflow.com)

I got the notion that a BeanFactory/ApplicatioContext can be initialized with several bean configuration files. Is that so? And, if it is how can it be done?

+2  A: 

See section 3.2.2.1 in the Spring Reference documentation. This describes how a configuration file can be split into separate configuration files that can then be imported into your main configuration file.

Mark
Thanks :) This pretty much answered my question.
Yaneeve
+4  A: 

Mark's answer is fine. You may also want to try this:

ApplicationContext context = new ClassPathXmlApplicationContext( new String[]{  
                               "services.xml", 
                               "daos.xml", 
                               "webservices.xml", 
                               "validators.xml"
                             });
dfa
I prefer this method since you don't have to explicitly "know" about where the other beans are defined. Makes it easy to set up different environments for testing, integration testing ...
Robin
+1  A: 

If you use an XML configuration file you can import multiple files from the classpath as such:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
  <import resource="classpath:/path/to/file/one.xml" />
  <import resource="classpath:/path/to/file/two.xml" />
</beans>
Ben Turner
I had preferred not to have the files aware of one another...
Yaneeve
If you load multiple files into one application context they are inherently aware of each other (i.e. you can reference beans defined in other files).
Ben Turner
A: 

While reading the above answers I had found the following class and its relevant constructor:

[FileSystemXmlApplicationContext][1]

public FileSystemXmlApplicationContext(String[] configLocations,
                                       boolean refresh,
                                       ApplicationContext parent)
                                throws BeansException

[1]: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/support/FileSystemXmlApplicationContext.html#FileSystemXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)

Yaneeve
There is also http://static.springframework.org/spring/docs/1.0.2/api/org/springframework/context/support/ClassPathXmlApplicationContext.html which supports the same capability.
Robin