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 into several sub bean configuration files, each one includes a special group of beans definitions. But how to load them in your java code, this article will show you methods.

1. Bean Configuration File Examples.

  1. There are totally five bean configuration XML files in this example, they are listed below.
    │  └─main
    │      └─resources
    │          │  AllSpringBeanSettings.xml
    │          │
    │          ├─beanfactory
    │          │      FactoryBeanSettings.xml
    │          │
    │          ├─common
    │          │      CommonBeanSettings.xml
    │          │
    │          ├─database
    │          │      DBBeanSettings.xml
    │          │
    │          └─sendmail
    │                  SendMailBeanSettings.xml
  2. beanfactory/FactoryBeanSettings.xml: Include spring factory examples objects.
  3. common/CommonBeanSettings.xml: Include commonly used objects.
  4. database/DBBeanSettings.xml: Include database connection related Spring beans.
  5. sendmail/SendMailBeanSettings.xml: Include objects used to send the email.
  6. AllSpringBeanSettings.xml: This XML file will import all the above bean configuration XML files.

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 the 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.

  1. In this example, the super bean configuration file is AllSpringBeanSettings.xml. To create it, please follow the below steps.
  2. Right-click src/main/resources folder in Eclipse java project left panel.
  3. Click ” New —> Others “.
  4. Choose the ” Spring Bean Configuration File ” item in the popup dialog.
  5. Click the Next button, input AllSpringBeanSettings.xml in the File Name input text box.
  6. Click the Finish button to complete.
  7. Use the 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"/>
  8. Now you can use the single super Spring bean configuration file to load multiple sub bean configurations.
    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"); 

Leave a Comment

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.