How to load multiple external bean configuration file in XML or programatically

Here I am creating three classes : Message1.java, Message2.java, Message2.java and respective Spring configuration file : beans1.xml, beans2.xml, beans3.xml. Finally creating final.xml configuration file which configure all three xml file created earlier.
Please follow the below steps :
  • Create POJO classes : Message1.java, Message2.java, Message3.java
  • Create Spring configuration files : src/techmyguru/spring/beans1.xml, src/techmyguru/spring/beans2.xml, src/techmyguru/spring/beans3.xml
  • Create Spring configuration file for integration of above XML file
  • Create MessageClient.java to create Container
  • Add Spring library in project classpath
  • Verify the directory structure and Run MessageClient.java

Create POJO classes and respective configuration file
Message1.java
package techmyguru.spring;
public class Message1 {
private String message1;
public String getMessage1() {
  return message1;
}
public void setMessage1(String message1) {
  this.message1 = message1;
}
}

src/techmyguru/spring/beans1.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">

<bean id="m1" class="techmyguru.spring.Message1">
<property name="message1" value="This is Message1" />
</bean>

</beans>

Message2.java
package techmyguru.spring;
public class Message2 {
private String message2;
public String getMessage2() {
  return message2;
}
public void setMessage2(String message2) {
  this.message2 = message2;
}
}

src/techmyguru/spring/beans2.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">

<bean id="m2" class="techmyguru.spring.Message2">
  <property name="message2" value="This is Message2" />
</bean>
</beans>


Message3.java
package techmyguru.spring;
public class Message3 {
private String message3;
public String getMessage3() {
  return message3;
}
public void setMessage3(String message3) {
  this.message3 = message3;
}
}

src/techmyguru/spring/beans3.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">

<bean id="m3" class="techmyguru.spring.Message3">
  <property name="message3" value="This is Message3" />
</bean>

</beans>


There is two way to use multiple configuration file :

Using configuration file

src/techmyguru/spring/final.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">

<import resource="bean1.xml" />
<import resource="bean2.xml" />
<import resource="bean3.xml" 
/>

</beans>

MessageClient.java
package techmyguru.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageClient
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext
    ("classpath:techmyguru/spring/final.xml");
Message1 m1=(Message1)ctx.getBean("m1");
Message2 m2=(Message2)ctx.getBean("m2");
Message3 m3=(Message3)ctx.getBean("m3");
System.out.println(m1.getMessage1());
System.out.println(m2.getMessage2());
System.out.println(m3.getMessage3());
}
}


Using programatically

MessageClient.java
package techmyguru.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageClient
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext
  (new String[]{"classpath:techmyguru/spring/bean1.xml",
  "classpath:techmyguru/spring/bean2.xml",
  "classpath:techmyguru/spring/bean3.xml"});

Message1 m1=(Message1)ctx.getBean("m1");
Message2 m2=(Message2)ctx.getBean("m2");
Message3 m3=(Message3)ctx.getBean("m3");
System.out.println(m1.getMessage1());
System.out.println(m2.getMessage2());
System.out.println(m3.getMessage3());
}
}

Add Spring library in project classpath : Add the following jar files.
commons-logging-1.0.4.jar
spring-tx-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-test-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar

Verify the directory structure :


Run the EmployeeClient.java and get output :


Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code