Yaneeve
Yaneeve

Reputation: 4779

How can I configure a Spring BeanFactory with several bean configuration files?

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?

Upvotes: 3

Views: 2807

Answers (4)

Yaneeve
Yaneeve

Reputation: 4779

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

FileSystemXmlApplicationContext

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

Upvotes: 0

Ben Turner
Ben Turner

Reputation: 131

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>

Upvotes: 1

dfa
dfa

Reputation: 116304

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"
                             });

Upvotes: 4

Mark
Mark

Reputation: 29119

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.

Upvotes: 2

Related Questions