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="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>org.netjs.exp</groupId>
      <artifactId>Spring-Example</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>Spring-Example</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.2.1.RELEASE</spring.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
      </dependencies>
    </project>
    

    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() {
      payment.executePayment();
     }
     
     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="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-4.0.xsd">
     
     <!-- 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" />
      </bean>
    </beans>
    
  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");
            bean.performPayment();
            context.close();
        }
    }
    

    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