Import multiple spring configuration file

Import multiple spring configuration file

If you are working on large project it’s good idea to break up main application context bean configuration to multiple files to make it easy to handle and maintain. Below is import tag which you can use to import multiple files:

<import resource="beam-configuration.xml"/>

Please see full details working example below:

  • Below is maven project structure:

1

  • Create Person.java interface inside com.javahonk.bean package
package com.javahonk.bean;

public interface Person {

    public String getName();
}

  • implements Person interface in class PersonImpl inside package com.javahonk.bean
package com.javahonk.bean;

public class PersonImpl implements Person {

    public String getName() {
        return "Java Honk";
    }

}

  • Create person.xml file inside resources folder and create bean definition inside it:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="personBean" class="com.javahonk.bean.PersonImpl" />

</beans>
  • Create application-context.xml file inside resources folder and import person.xml bean file :
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="person.xml"/>

</beans>

  • Now create BeanImportTest.java to load XML file and test configuration inside com.javahonk package
package com.javahonk;

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

import com.javahonk.bean.Person;

public class BeanImportTest {

    public static void main(String[] args) {

        
        System.out.println("Load application context through XML file");        
        ClassPathXmlApplicationContext applilcationContext = 
                new ClassPathXmlApplicationContext(
                    "application-config.xml");
        Person person2 = (Person) applilcationContext.getBean("personBean");

        System.out.println("Name: "+person2.getName());
        ((ConfigurableApplicationContext)applilcationContext).close();

    }

}

  • To run — right click BeanImportTest.java file –> Run As –> Java Application you will see below output on console:

Import multiple spring configuration file

Leave a Reply

Your email address will not be published. Required fields are marked *