Spring Boot XML Configuration Example - RameshMF/spring-boot-developers-guide GitHub Wiki
In this article, we will discuss how to do XML based configuration in Spring Boot applications.
Spring provides @ImportResource annotation to import one or more XML context files. @ImportResource provides similar functionality as that of element. Based on the file extension the appropriate reader will be used to process the context files. For example, if it is a groovy file, a groovy reader GroovyBeanDefinitionReader will be used. If the context is an xml, an xml reader XmlBeanDefinitionReader will be used.
In this example, we are creating a simple message processing spring boot application . Here we are sending a message using different services like SMSService, TwitterService, and MAILService. We will configure message service beans in applicationContext.xml file and we will load using @ImportResource annotation as:
@SpringBootApplication
@ImportResource({"classpath*:applicationContext.xml"})
public class Springboot2XmlConfigApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Springboot2XmlConfigApplication.class, args);
MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
userService.processMsg("twitter message sending ");
}
}
While there are multiple ways of doing this, the recommended way is to create a separate configuration class to load this xml bean definition file.
@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}
The key part of the definition is @ImportResource({"classpath*:applicationContext.xml"}). applicationContext.xml will be imported from the classpath.
Let's create a complete simple spring boot example to demonstrate how to set up XML based configuration.
- Spring Boot - 2.0.4.RELEASE
- JDK - 1.8 or later
- Spring Framework - 5.0.8 RELEASE
- Maven - 3.2+
- IDE - Eclipse or Spring Tool Suite (STS)
There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.
Please refer the project structure as in below diagram.
diagram here
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.guides.springboot2</groupId>
<artifactId>springboot2-xml-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot2-xml-config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Let's create Message POJO class with few service class for demonstration. In this example, we are sending messages using different services like SMSService, TwitterService and MAILService.
package net.guides.springboot2.springboot2xmlconfig.model;
public class Message {
private int id;
private String message;
public Message(int id, String message) {
super();
this.id = id;
this.message = message;
}
}
public interface MessageService {
public void sendMsg(String message);
}
import org.springframework.stereotype.Service;
public class EmailService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
import org.springframework.stereotype.Service;
public class SMSService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
import org.springframework.stereotype.Service;
public class TwitterService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
public interface MessageService {
public void sendMsg(String message);
}
public interface MessageProcessor {
public void processMsg(String message);
}
package net.guides.springboot2.springboot2xmlconfig.service;
public class MessageProcessorImpl implements MessageProcessor {
private MessageService messageService;
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public void processMsg(String message) {
messageService.sendMsg(message);
}
}
<?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="emailService"
class="net.guides.springboot2.springboot2xmlconfig.service.EmailService" />
<bean id="sMSService"
class="net.guides.springboot2.springboot2xmlconfig.service.SMSService" />
<bean id="twitterService"
class="net.guides.springboot2.springboot2xmlconfig.service.TwitterService" />
<bean id="messageProcessor"
class="net.guides.springboot2.springboot2xmlconfig.service.MessageProcessorImpl">
<property name="messageService" ref="twitterService"></property>
</bean>
</beans>
This spring boot application has an entry point Java class called Springboot2XmlConfigApplication.java with the public static void main(String[] args) method, which you can run to start the application.
package net.guides.springboot2.springboot2xmlconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
import net.guides.springboot2.springboot2xmlconfig.service.MessageProcessor;
@SpringBootApplication
@ImportResource({"classpath*:applicationContext.xml"})
public class Springboot2XmlConfigApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Springboot2XmlConfigApplication.class, args);
MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
userService.processMsg("twitter message sending ");
}
}