Sunday, August 4, 2019

Spring XML Configuration Example Program

In this post we'll see a Spring XML configuration example program. Though HelloWorld program is a good starting point but better to go with a Spring XML configuration 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 5.1.8
  • Java 12
  • Apache Maven
  • Eclipse IDE

Provide Maven dependencies in pom.xml

<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</groupId>
  <artifactId>SpringConfig</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <description>Project for Spring cofiguration examples</description>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <spring.version>5.1.8.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 need to add dependencies for spring-core and spring-context and an entry for spring.version in properties element.

Spring XML configuration example - Java classes

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

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

Payment Service

Payment Service class has dependency on instance of type IPayment, that dependency will be defined in the XML configuration.

public class PayServiceImpl {
 private IPayment payment;
 public void performPayment() {
  payment.executePayment();
 }
  
 public IPayment getPayment() {
  return payment;
 }
 
 public void setPayment(IPayment payment) {
  this.payment = payment;
 }
}

Spring XML Configuration

Create a XML file appcontext.xml under "src/main/resources" and provide bean definitions and bean dependencies in the created XML file. This XML file is used while creating application context.

<?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">
    <!-- defining CashPayment bean -->
    <bean id="cashPaymentBean" class="springbeans.CashPayment" />
 
    <!-- Defining PayServiceImpl bean and injecting payment bean -->
    <bean id="payServiceBean" class="springbeans.PayServiceImpl">
        <property name="payment" ref="cashPaymentBean" />
    </bean>
</beans>

Java class to run the application

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

 public static void main(String[] args) {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");
  PayServiceImpl payBean = context.getBean("payServiceBean", PayServiceImpl.class);
  payBean.performPayment();
  context.close();
 }
}

Note that ClassPathXmlApplicationContext class is used here to create application context. 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.

we need to pass the name of the XML configuration file while creating object of ClassPathXmlApplicationContext class. Just ensure that the XML file is in the class path. Then we can use this application context to get the bean using the id provided in the XML config file.

Output

19:07:02.923 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 
Loaded 2 bean definitions from class path resource [appcontext.xml]
19:07:03.100 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 
Creating shared instance of singleton bean 'cashPaymentBean'
19:07:03.132 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 
Creating shared instance of singleton bean 'payServiceBean'
Perform Cash Payment 

Project structure for this Spring XML configuration example program will look like this.

That's all for this topic Spring XML Configuration Example Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page


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. How to Read Properties File in Spring Framework
  5. Wiring Collections in Spring

You may also like-

  1. Different Bean Scopes in Spring
  2. Autowiring Using XML Configuration in Spring
  3. Insert\Update Using NamedParameterJDBCTemplate in Spring Framework
  4. Creating a Maven project in Eclipse
  5. Constructor overloading in Java
  6. How ArrayList works internally in Java
  7. How HashMap internally works in Java
  8. CountDownLatch in Java concurrency

No comments:

Post a Comment