Spring Framework XML Configuration Example

In this tutorial, we will learn how to create a simple Spring application that configures XML-based configuration.

In this tutorial, we will create a Spring project (without Spring Boot) with Spring core dependencies.

1. Create a simple Maven Project

Create a simple maven project using your favorite IDE. If you are new to maven then read this article How to Create a Simple Maven Project.

2. Add Maven Dependencies - pom.xml

Add the following content to the pom.xml file:
<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>com.javadevsguide.springframework</groupId>
  <artifactId>spring-xmlbased-config</artifactId>
  <version>0.0.1-SNAPSHOT</version>
	<properties>
		<maven.compiler.target>17</maven.compiler.target>
		<maven.compiler.source>17</maven.compiler.source>
	</properties>
  <dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>6.0.6</version>
		</dependency>
	</dependencies>
</project>

3. Create Java Interfaces and Classes

MessageService

public interface MessageService {
	public void sendMsg(String message);
}

TwitterService

public class TwitterService implements MessageService{

	public void sendMsg(String message) {
		System.out.println(message);
	}
}

MessageProcessor

public interface MessageProcessor {
	public void processMsg(String message);
}

MessageProcessorImpl

public class MessageProcessorImpl implements MessageProcessor {

	private MessageService messageService;

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public void processMsg(String message) {
		messageService.sendMsg(message);
	}
}

4. XML-Based Configuration

Let's create Spring beans for TwitterService and MessageProcessorImpl using XML-based configuration.

Let's create an applicationContext.xml file and add the following content to it:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="twitterService" class="com.javadevsguide.springframework.service.TwitterService"></bean>
	
	<bean id="messageProcessor" class="com.javadevsguide.springframework.service.MessageProcessorImpl">
		<property name="messageService" ref="twitterService"></property>
	</bean>
</beans>

5. Testing

Let's create ApplicationContext and retrieve Spring beans from it:
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javadevsguide.springframework.service.MessageProcessor;
import com.javadevsguide.springframework.service.MessageProcessorImpl;

public class TestApplication {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"application-context.xml");
		MessageProcessor  userService = applicationContext.getBean(MessageProcessorImpl.class);
		userService.processMsg("twitter message sending ");
		applicationContext.close();
	}
}
Output:
twitter message sending 

GitHub

The source code of this example is available on my GitHub repository.

Comments