Import Multiple Spring XML Configuration Files

This tutorial shows you how to import multiple spring xml configuration files. When your application is composed of multiple layers it is recommended that each XML Configuration file represents a logical layer or module in your application. This makes the configuration code more maintainable. You can use the ApplicationContext constructor to load bean definitions from multiple XML configuration files. This constructor takes an Array of strings representing the Resource locations. Or, like we see in this tutorial, you can also use the <import/> element to load bean definitions from other files.

Application Services

We are structuring our application in multiple layers. The first layer we introduce is the service-layer. We create a CoffeeService that has a makeCoffee method. Later on, we are going to configure this service as a spring bean.

package com.memorynotfound.spring;

public class CoffeeService {

    public void makeCoffee(String beans) {
        System.out.println("Making " + beans + " coffee!");
    }

}

Next we have our CoffeeMachine. This class uses the CoffeeService to make some coffee.

package com.memorynotfound.spring;

public class CoffeeMachine {

    private CoffeeService coffeeService;

    public void makeCoffee(String beans) {
       coffeeService.makeCoffee(beans);
    }

    public void setCoffeeService(CoffeeService coffeeService) {
        this.coffeeService = coffeeService;
    }
}

Service Layer XML Configuration

We create a bean definition using the <bean> element. We provide the fully qualified class name of the CoffeeService and provide an id attribute, which we can later use to identify our bean.

<?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="coffeeService" class="com.memorynotfound.spring.CoffeeService"/>

</beans>

Application Layer XML Configuration

We can import the service-config.xml file using the <import/> element. This element will load the external bean definitions from the given location. This location path must be relative to the definition file doing the importing. In other words, this file must be in the same directory or classpath location as the file doing the importing. After the import we create another bean definition. We inject the previously created CoffeeService into the CoffeeMachine bean using setter-dependency-injection.

<?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>
        This tutorial demonstrates how to import multiple spring xml configuration files
    </description>

    <import resource="service-config.xml"/>

    <bean class="com.memorynotfound.spring.CoffeeMachine">
        <property name="coffeeService" ref="coffeeService"/>
    </bean>

</beans>

Bootstrap Spring Application

We could load all the bean definition files using the constructor of the ApplicationContext by providing constructor arguments new String[]{"app-config.xml","service-config.xml"}. But instead we used the <import/> element.

package com.memorynotfound.spring;

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

public class Run {

    public static void main(String... args){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"app-config.xml"});
        CoffeeMachine coffeeMachine = context.getBean(CoffeeMachine.class);
        coffeeMachine.makeCoffee("Java");
    }
}

Demo

After running the application, the console will display the following result.

Making Java coffee!

References

Download

You may also like...