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:
beanfactory/FactoryBeanSettings.xml
: Include spring factory examples objects.common/CommonBeanSettings.xml
: Include common used objects.database/DBBeanSettings.xml
: Include database connection related Spring beans.sendmail/SendMailBeanSettings.xml
: Include objects which used to send email.AllSpringBeanSettings.xml
: This xml will import all above bean configuration file.
2. Load Multiple Bean Configuration File Use String Array.
- 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. - 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.
- Right click
src/main/resources
folder in Eclipse java project left panel. - Click ” New —> Others “.
- Choose ” Spring Bean Configuration File ” item in the popup dialog.
- Click Next, input
AllSpringBeanSettings.xml
in File name input text box.
- Click Finish button to complete.
- 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"/>
- 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 id=”2861″]