Monday, 12 October 2015

Spring example program using XML configuration

In this post I'll talk about how to create a Spring example program using XML configuration. Though HelloWorld is a good starting point but better to go with an example which has at least two classes so that you can get idea about how those classes are defined in XML configuration and how dependencies are defined.
In this tutorial Maven is used to create a general Java project structure.

Technologies used

  • Spring 4.2.1
  • JDK 1.8
  • Apache Maven 3.3.3
  • Eclipse Luna 4.2.2

In this Spring program there are 2 classes; first is Payment Service and another is Cash Payment class, let's go step by step to see how these classes are configured in XML and how they are used.

  1. Provide dependencies in pom.xml of Maven

    <project xmlns="" 

    Note that you'll already have most of the entries in pom.xml, you just need to add dependencies for spring-core and spring-context and an entry for spring.version in properties element.

  2. Create Java classes

    We need 2 classes PaymentService and CashPayment. Since Spring promotes loose coupling so it is always recommended to code to interfaces. So we'll also create interfaces for the classes.

    Payment Service

    public interface IPayService {
     void performPayment();
    public class PayServiceImpl implements IPayService {
     private IPayment payment;
     public void performPayment() {
     public IPayment getPayment() {
      return payment;
     public void setPayment(IPayment payment) {
      this.payment = payment;

    Cash Payment

    public interface IPayment {
     void executePayment();
    public class CashPayment implements IPayment{
     public void executePayment() {
      System.out.println("Perform Cash Payment "); 
  3. Spring XML Configuration

    Let's create a new source folder resources and save XML config there.
    To create source folder right click the Java project Spring-Example and select source folder.

    Spring example program using XML config

    In the next window provide the folder name and click finish.

    Spring example program using XML config

    Within the created folder create a XML file appcontext.xml and write the configuration for the beans in the created XML file.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
     <!-- defining CashPayment bean -->
      <bean id="cashPaymentBean" class="org.netjs.exp.Spring_Example.CashPayment" />
      <!-- Defining PayServiceImpl bean and injecting payment bean -->
      <bean id="payServiceBean" class="org.netjs.exp.Spring_Example.PayServiceImpl">
          <property name="payment" ref="cashPaymentBean" />
  4. Java class to run the application

    public class App 
        public static void main( String[] args ){
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
            IPayService bean = (IPayService) context.getBean("payServiceBean");

    Here we need to give the name of the XML configuration file. Then we can get the bean using the id provided in the XML config file.

Created project structure will look like this.

Spring example program using XML config

That's all for this topic Spring example program using XML configuration. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Spring example program using JavaConfig and Annotations
  2. Spring example program using automatic configuration
  3. Constructor-based dependency injection in Spring
  4. Different bean scopes in Spring
  5. Wiring collections in Spring
  6. Autowiring using XML configuration in Spring

You may also like -

>>>Go to Spring tutorial page

No comments:

Post a Comment