🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment