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
@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.<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>2. Create Interfaces and Classes
In this example, we are sending messages using different services like SMSService, TwitterService, and EmailService.MessageService.java
public interface MessageService {
public void sendMsg(String message);
}EmailService.java
public class EmailService implements MessageService {
public void sendMsg(String message) {
System.out.println(message);
}
}SMSService.java
public class SMSService implements MessageService {
public void sendMsg(String message) {
System.out.println(message);
}
}TwitterService.java
public class TwitterService implements MessageService {
public void sendMsg(String message) {
System.out.println(message);
}
}MessageProcessor.java
public interface MessageProcessor {
public void processMsg(String message);
}MessageProcessorImpl.java
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
<?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
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment