Sometimes it necessary to integrate XML configuration into Spring Boot application. For example, some of your beans already defined in existing XML. In this tutorial we are going to see how to define beans in XML and use them in Spring Boot application.
@ImportResource annotation will be used to load one or more XML configuration files into another configuration.
1. Defining Beans in XML
First let’s create SampleBean class and define that bean in XML and use it Spring application. The following is beans-context.xml
sample XML beans definitions file.
public class SampleBean { private String name; private String value; // setters, getters and toString }
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="com.javabydeveloper.spring.bean.SampleBean"> <property name="name" value="Student"></property> <property name="value" value="Peter"></property> </bean> </beans>
2. Using @ImportResource annotation
@ImportResorce indicates one or more resources containing bean definitions to import into Spring Application Context. We need to tell that where to find XML resource. In following example ImportResourceDemoService bean defined to test configured XML beans.
@Configuration @ImportResource("classpath:beans-context.xml") public class AppConfigForXmlBeans { @Bean public ImportResourceDemoService getImportResourceDemoService() { return new ImportResourceDemoService(); } }
public class ImportResourceDemoService { @Autowired private SampleBean sampleBean; public void printBeans() { System.out.println(sampleBean); } }
Testing configuration:
public class ApplicationImportResourceDemo { public static void main(String[] args) { ApplicationContext ctxt = new AnnotationConfigApplicationContext(AppConfigForXmlBeans.class); ImportResourceDemoService irds = ctxt.getBean(ImportResourceDemoService.class); irds.printBeans(); } }
Output:
SampleBean [name=Student, value=Peter]
3. @ImportResource with Spring Boot
@ImportResource can be used with @SpringBootApplication annotation in Spring Boot application.
@SpringBootApplication @ImportResource("classpath:beans-context.xml") public class SpringBootXmlBeansDemoApp implements CommandLineRunner{ public static void main(String[] args) { ConfigurableApplicationContext ctxt = SpringApplication.run(SpringBootXmlBeansDemoApp.class, args); SampleBean sampleBean = ctxt.getBean(SampleBean.class); System.out.println("*********"+sampleBean+"*********"); } }
Output:
*********SampleBean [name=Student, value=Peter]*********
4. Defining Bean with properties
For example, if want to access properties defined in application.properties file into XML:
examplebean.key=Employee examplebean.value=Peter Milanovich
4.1. Accessing properties in XML in Spring Boot Application: In Spring Boot application, properties defined in application.properties can be accessed directly in XML. Let’s define another bean called Example in 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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> // ... other beans <bean class="com.javabydeveloper.spring.bean.ExampleBean"> <property name="key" value="${examplebean.key}"></property> <property name="value" value="${examplebean.value}"></property> </bean> </beans>
Testing:
@SpringBootApplication @ImportResource("classpath:beans-context.xml") public class SpringBootXmlBeansDemoApp implements CommandLineRunner{ @Autowired private ExampleBean exampleBean; public static void main(String[] args) { ConfigurableApplicationContext ctxt = SpringApplication.run(SpringBootXmlBeansDemoApp.class, args); // .... } @Override public void run(String... args) throws Exception { System.out.println("-----"+exampleBean+"---------"); } }
Output:
-----ExampleBean [key=Employee, value=Peter Milanovich]---------
4.2. Accessing properties in XML if not using Spring Boot: In Spring application (without Spring Boot), to access properties defined in properties file within XML, you need to import properties resource file into XML using <context:property-placeholder … />. Let’s see sample XML 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:application.properties" /> <bean class="com.javabydeveloper.spring.bean.SampleBean"> <property name="name" value="Student"></property> <property name="value" value="Peter"></property> </bean> <bean class="com.javabydeveloper.spring.bean.ExampleBean"> <property name="key" value="${examplebean.key}"></property> <property name="value" value="${examplebean.value}"></property> </bean> </beans>
5. Conclusion
In this tutorial, we have seen how to inject beans defined in XML files in Spring Boot application using with @ImportResource annotation.
You can get source code from our github repository.
6. References
Other Related Tutorials:
- Spring @Order
- Spring @Lazy
- Spring @Autowired
- Spring Injecting Collection
- Spring @Primary
- Spring @Import
- Spring Component Scanning
- Spring @ComponentScan Filter Types