April 10, 2022

Spring XML Configuration Example

In this Spring XML configuration example we’ll see how to create a Spring application where bean declaration and bean dependencies configuration is done using XML configuration.

For this Spring XML configuration example Maven build tool is used for managing dependencies. Please refer Create Java Project Using Maven in Eclipse to see how to create a Maven project.

Maven dependencies

For this example we need spring core and spring context dependencies. Spring Version used is 5.1.8 Release which is defined under properties element in pom.xml.

  • Spring core provides the basic framework classes and classes to interact with other modules.
  • Spring context module provides org.springframework.context.ApplicationContext interface which represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.
<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">




Bean classes

In the example there is a class to place orders called Order and purchase can be done from a store. Order and Store beans and bean dependencies will be configured using Spring XML configuration.

public interface IStore {
  public void doPurchase(int items);
public class Store implements IStore {
  public void doPurchase(int items) {
    System.out.println("Doing purchase of " + items +" Items");
public class Order {
  private IStore store;
  private int items;
  public Order(IStore store, int items) {
    this.store = store;
    this.items = items;
  public void setStore(IStore store) {
    this.store = store;

  public void buyItems() {

As you can see from the classes Order has a dependency on Store (store field in Order class which is of type IStore).

Spring XML Configuration

Create an XML file appContext.xml in src/main/resources package. This XML file is used for creating ApplicationContext.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  <!-- Store bean --> 
  <bean id="store" class="com.knpcode.SpringProject.Store" />           
  <!-- Order bean with dependencies -->
  <bean id="order" class="com.knpcode.SpringProject.Order">
    <constructor-arg ref="store" />
    <constructor-arg type="int" value="20" />
Note that here dependencies are injected as constructor dependency injection.

You can use the following class with main method to read the configuration and call the bean method. There are several implementations of the ApplicationContext interface in Spring framework. In standalone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
  public static void main( String[] args ){
    // create context using configuration
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
    Order order = context.getBean("order", Order.class);
    // close the context
17:48:34.049 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader
- Loaded 2 bean definitions from class path resource [appcontext.xml]
17:48:34.173 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 
Creating shared instance of singleton bean 'store'
17:48:34.217 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 
Creating shared instance of singleton bean 'order'
Doing purchase of 20 Items

That's all for the topic Spring XML Configuration Example. If something is missing or you have something to share about the topic please write a comment.

You may also like

No comments:

Post a Comment