How To Load Multiple Spring Bean Configuration Files Example

In Large Spring aplications you may have different configuration files for different Layers Like Dao Layes and Service Layer.
In This Tutorial We Will Learn How TO Load Multiple spring configuration Files with Examples.
we can achieve this by folowing ways
  • import tag
  • applicationContext Constructor

Lets See Example one by one.First We will See by <import> tag.

<import> tag

In this Following example i have two springs called Series.java and Telivision.java and we have two spring bean configuration files called seriesapplicationContext.xml and telivisionapplicationContext.xml, in applicationContext.xml we have to import the above two spring bean configuration Files.

Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.7

Project Directory Structure

Maven Dependencies

Add The Following Dependencies to the pom.xml
  • spring-core
  • spring-beans
  • spring-context
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.practice</groupId>
<artifactId>scopeanotationDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>


</dependencies>
</project>

2.Writing Spring Beans

Series.java

package com.practice;

public class Series {
private int seriesNumber;

public int getSeriesNumber() {
return seriesNumber;
}


public void setSeriesNumber(int seriesNumber) {
this.seriesNumber = seriesNumber;
}


@Override
public String toString() {
return "Series [seriesNumber=" + seriesNumber + "]";
}


}

Telivision.java

package com.practice;

public class Telivision {
private String name;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Telivision [name=" + name + "]";
}



}

Spring Configuration Files

seriesaplicationContext.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

<bean id = "seriesBean" class = "com.practice.Series">
<property name = "seriesNumber" value = "1000"></property>
</bean>
</beans>
telivisionaplicationContext.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id = "telivisionBean" class = "com.practice.Telivision">
<property name = "name" value = "AAAAA"></property>
</bean>
</beans>
applicationContext.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">

<import resource = "seriesapplicationContext.xml"/>
<import resource = "telivisionapplicationContext.xml"/>
</beans>

Now Write a Test Class and Run The Code

AppTest.java

package com.practice.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.practice.Series;
import com.practice.Telivision;

public class AppTest {


public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Telivision telivision = (Telivision)context.getBean("telivisionBean");
System.out.println(telivision.toString());
System.out.println("------------------");
Series series = (Series)context.getBean("seriesBean");
System.out.println(series.toString());


}

}

output


INFO: Loading XML bean definitions from class path resource [telivisionapplicationContext.xml]
Telivision [name=AAAAA]
------------------
Series [seriesNumber=1000]

Now Let's See another way by using applicationContext Constructor

First We need to create multiple spring ban configuration files and create a String array and pass it to the applicationContext constructor.

For that we need to change the following code in Test class(AppTest.java) And Run The code Again

AppTest.java

package com.practice.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.practice.Series;
import com.practice.Telivision;

public class AppTest {


public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"seriesapplicationContext.xml","telivisionapplicationContext.xml"});
Telivision telivision = (Telivision)context.getBean("telivisionBean");
System.out.println(telivision.toString());
System.out.println("------------------");
Series series = (Series)context.getBean("seriesBean");
System.out.println(series.toString());


}

}

output:


Sep 02, 2016 10:19:03 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1887dd5: startup date [Fri Sep 02 22:19:03 IST 2016]; root of context hierarchy
Sep 02, 2016 10:19:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [seriesapplicationContext.xml]
Sep 02, 2016 10:19:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [telivisionapplicationContext.xml]
Telivision [name=AAAAA]
------------------
Series [seriesNumber=1000]