In this article, we will quickly discuss how to develop a simple Spring boot application using XML-based Spring configuration.
In this example, we don't use either Annotation-based configuration or Java-based configuration, we only use XML-based configuration to create and configure beans.
In this example, we don't use either Annotation-based configuration or Java-based configuration, we only use XML-based configuration to create and configure beans.
Overview
Spring provides a @ImportResource annotation to load beans from an applicationContext.xml file into an Application Context.
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 EmailService. We will configure message service beans in the applicationContext.xml file and we will load beans using @ImportResource annotation as:
@ImportResource({"classpath*:applicationContext.xml"})
@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 {
}
Let's create a complete simple spring boot example to demonstrate how to set up an XML-based configuration.
1. Setup
Let's quickly create a Spring Boot application using Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.Add this Maven dependency in the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Here is the project structure or packaging structure for your reference:
2. Create Interfaces and Classes
In this example, we are sending messages using different services like SMSService, TwitterService, and EmailService.MessageService.java
Let's create a MessageService interface with the following content:
public interface MessageService {
public void sendMsg(String message);
}
EmailService.java
Let's create an EmailService class that implements the MessageService interface and its method:
public class EmailService implements MessageService {
public void sendMsg(String message) {
System.out.println(message);
}
}
SMSService.java
Let's create the SMSService class that implements the MessageService interface and its method:
public class SMSService implements MessageService {
public void sendMsg(String message) {
System.out.println(message);
}
}
TwitterService.java
Let's create the TwitterService class that implements the MessageService interface and its method:
public class TwitterService implements MessageService {
public void sendMsg(String message) {
System.out.println(message);
}
}
MessageProcessor.java
Let's create a MessageProcessor interface with the following content:
public interface MessageProcessor {
public void processMsg(String message);
}
MessageProcessorImpl.java
Let's create the MessageProcessorImpl class that implements the MessageProcessor interface and its method:
public class MessageProcessorImpl implements MessageProcessor {
private MessageService messageService;
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public void processMsg(String message) {
messageService.sendMsg(message);
}
}
3. The applicationContext.xml File
Let's create and configure Spring beans in the applicationContext.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">
<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>
4. Running Application
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.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 ");
}
}
Output:
Master Spring Boot with Source Code on GitHub - Spring Boot Tutorial
Comments
Post a Comment
Leave Comment