Spring Boot 3 primarily favors annotation-based configuration for its simplicity and maintainability. However, there are scenarios where you might need to integrate existing XML configurations into your application. This tutorial demonstrates how to achieve this using a @Configuration
class and the @ImportResource
annotation.
Firstly, if you are new to Spring Boot Configuration, check this article: Configuring Spring Boot applications
Project Setup:
- Create a new Spring Boot project using your preferred IDE or Spring Initializr.
- Within the
src/main/java
directory, create the following packages:com.example.demo
Java Classes:
Create a Java class named MyService.java
under com.example.demo
:
package com.example.demo; public class MyService { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
Create a Java class named XmlConfiguration.java
under com.example.demo
:
package com.example.demo; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource({"classpath*:applicationContext.xml"}) public class XmlConfiguration { }
Define Spring Boot XML Configuration
Create an XML file named applicationContext.xml
under src/main/resources
:
<?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 id="myService" class="com.example.demo.MyService"> <property name="message" value="Hello from XML configuration!" /> </bean> </beans>
Explanation:
XmlConfiguration.java
:@Configuration
: Marks this class as a Spring configuration class.@ImportResource
: This annotation instructs Spring Boot to import bean definitions from the specified resource(s). In this case, we referenceclasspath:applicationContext.xml
.
applicationContext.xml
:- XML schema declarations: Specify the locations of Spring Bean schema files.
<bean id="myService">
: Defines a bean with the ID “myService” and specifies its class ascom.example.demo.MyService
.<property name="message">
: Sets the “message” property of the bean with the specified value.
Here is the project structure:
Running the Application:
- Ensure you have Maven or Gradle configured for your project.
- Run the Spring Boot application using your preferred method (e.g.,
mvn spring-boot:run
for Maven).
Verification:
Use your favorite debugger or logging framework to verify that the MyService
bean is indeed created and configured with the message “Hello from XML configuration!”.
Important Note:
While XML configuration offers flexibility, Spring Boot encourages the use of annotation-based configuration for its advantages in maintainability and IDE support. Consider migrating existing XML configurations to annotations whenever possible.
Found the article helpful? if so please follow us on Socials