The loading multiple Spring beans configuration files example shows you how to import multiple spring xml configuration files. When you create a application with multiple layers. I strongly recommend you to configure each logical layer or module in a Spring XMl configuration file in your application. This configuration helps your source code more maintainable. Finally, You can use the ApplicationContext constructor to load bean definitions from an array of XML configuration filenames. Or you can also use the
Application Data Access Object(DAO) layer
We are structuring the application in multiple layers. First of all, We create the Data Access Object(DAO) layer with a simple class HelloDAO.
1 2 3 4 5 6 7 8 9 10 |
package com.javabycode.spring; public class HelloDAO { public String getMsgHello() { // do stuffs return "Spring multiple bean configuration files!"; } } |
Now, We are going to configure this DAO layer as a spring bean in a Spring XML configuration file named dao-config.xml, like below:
1 2 3 4 5 6 7 8 9 |
<?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.xsd"> <bean id="helloDAO" class="com.javabycode.spring.HelloDAO"/> </beans> |
Application Service layer
Next, we are creating Service layer with HelloService class. This class uses HelloDAO to say a message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javabycode.spring; public class HelloService { private HelloDAO helloDAO; public void setHelloDAO(HelloDAO helloDAO) { this.helloDAO = helloDAO; } public HelloDAO getHelloDAO() { return helloDAO; } public void sayHello() { String msg = helloDAO.getMsgHello(); System.out.println("Hello "+msg); } } |
Now, We are going to configure this Service layer as a spring bean in a Spring XML configuration file named service-config.xml, like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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.xsd"> <bean class="com.javabycode.spring.HelloService"> <!-- inject the previously created HelloDAO into the HelloService bean using setter-dependency-injection --> <property name="helloDAO" ref="helloDAO"/> </bean> </beans> |
Application Layer XML Configuration
We are creating a Application Layer XML Configuration file named spring-config.xml. This file will including two above XML Configuration files by using the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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.xsd"> <description> Spring multiple bean configuration files </description> <import resource="dao-config.xml"/> <import resource="service-config.xml"/> </beans> |
Demo Application
In this Loading multiple Spring beans configuration files example, we create main class and load all the bean definitions using the constructor of the ApplicationContext. The constructor arguments contain one Appication XML Configuration because we are using the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javabycode.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApplication { public static void main(String... args){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"}); //We can use the constructor arguments with array of multiple filenames instead we used the <import/> element //ApplicationContext context = new ClassPathXmlApplicationContext(String[]{"app-config.xml","service-config.xml"}); HelloService helloService = context.getBean(HelloService.class); helloService.sayHello(); } } |
Run the above main, the output should display like below:
1 |
Hello Spring multiple bean configuration files! |
That’s all on the tutorial Loading multiple Spring beans configuration files example.
References
ClassPathXmlApplicationContext JavaDoc
Spring Reference Documentation
Download the complete source code, click link below
spring-multiple-bean-configuration-files.zip (259 downloads)