Load Spring Beans From Multiple Configuration File

When your Spring project becomes bigger and bigger, the Spring bean configuration file may become very large. So you have to split it to several sub bean configuration file, each one include a special group of beans definition. But how to load them in your java code, this article will show you methods.

1. Bean Configuration File Examples.

There are totally five bean configuration xml file in this example, they are:

multiple spring bean configuration file

  1. beanfactory/FactoryBeanSettings.xml: Include spring factory examples objects.
  2. common/CommonBeanSettings.xml: Include common used objects.
  3. database/DBBeanSettings.xml: Include database connection related Spring beans.
  4. sendmail/SendMailBeanSettings.xml: Include objects which used to send email.
  5. AllSpringBeanSettings.xml: This xml will import all above bean configuration file.

2. Load Multiple Bean Configuration File Use String Array. 

  1. Save needed bean configuration xml file name(relative to the classpath root, in this example the classpath root is src/main/resources) in a String array.
  2. Create ApplicationContext object with above String array, then you can get all beans defined in the xml files by their id or name.
	public static void main(String[] args) {
		// Declare a String array to save all bean configuration file name as items.
		String beanSettingsArr[] = {"beanfactory/FactoryBeanSettings.xml", "common/CommonBeanSettings.xml"};
		// Initiate Spring application context use above array.
		ApplicationContext springAppContext = new ClassPathXmlApplicationContext(beanSettingsArr);

		// Below two object defined in beanfactory/FactoryBeanSettings.xml.
		DBConnectionDTO dbConnMysql = (DBConnectionDTO)springAppContext.getBean("mysqlDBConn");
		DBConnectionDTO dbConnOracle = (DBConnectionDTO)springAppContext.getBean("oracleDBConn");

                // Get object defined in common/CommonBeanSettings.xml.
                HelloWorldExample instance = (HelloWorldExample) springContext.getBean("helloWorldBean"); 
                /* Invoke the object's method. */ 

3. Include Sub Bean Configuration File In Super Bean Configuration File.

In this example, the super bean configuration file is AllSpringBeanSettings.xml. To create it, please follow below steps.

  1. Right click src/main/resources folder in Eclipse java project left panel.
  2. Click ” New —> Others “.
  3. Choose ” Spring Bean Configuration File ” item in the popup dialog.
    create spring bean configuration file
  4. Click Next, input AllSpringBeanSettings.xml in File name input text box.
    input spring bean configuration file name
  5. Click Finish button to complete.
  6. Use below xml tag to import sub bean configuration files into AllSpringBeanSettings.xml.
     <import resource="beanfactory/FactoryBeanSettings.xml"/>
     <import resource="common/CommonBeanSettings.xml"/> 
     <import resource="sendmail/SendMailBeanSettings.xml"/> 
     <import resource="database/DBBeanSettings.xml"/>
  7. Now you can use the single super Spring bean configuration file to load multiple sub bean configuration.
	public static void main(String[] args) {
		ApplicationContext springAppContext = new ClassPathXmlApplicationContext("AllSpringBeanSettings.xml");

		// Get object defined in common/CommonBeanSettings.xml. 
		StudentBean studentBean = (StudentBean) springAppContext.getBean("studentBean");
                System.out.println("Student name : " + studentBean.getStudentName());
                System.out.println("Student age : " + studentBean.getStudentAge());
                System.out.println("High School name : " + studentBean.getHighSchoolBean().getSchoolName());
                // Get object defined in database/DBBeanSettings.xml
                Connection postgresqlConn = (Connection) springAppContext.getBean("postgresqlDBConnection"); 
                String postgresqldbProductName = postgresqlConn.getMetaData().getDatabaseProductName(); 
                System.out.println("postgresqldbProductName = " + postgresqldbProductName); 

                // Get object defined in beanfactory/FactoryBeanSettings.xml
                CarFactory carFactory = (CarFactory)springAppContext.getBean("carFactoryBean"); 

                // Get object defined in sendmail/SendMailBeanSettings.xml
                SendEmailTool sendEmailTool = (SendEmailTool)springAppContext.getBean("sendEmailTool"); 

[download id=”2861″]

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x