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");
		System.out.println(dbConnMysql.toString());
		
		DBConnectionDTO dbConnOracle = (DBConnectionDTO)springAppContext.getBean("oracleDBConn");
		System.out.println(dbConnOracle.toString());

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

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"); 
                System.out.println(carFactory.getAudiBrand().toString());

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

Download “Load-Spring-Beans-From-Multiple-Bean-Configuration-File.zip” Load-Spring-Beans-From-Multiple-Bean-Configuration-File.zip – Downloaded 64 times – 4 KB

(Visited 458 times, 1 visits today)
READ :   Spring Dependency Injection Explanation Use Loosely Coupled Objects

Leave a Reply

Your email address will not be published. Required fields are marked *

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