Open In App

XML – Based Injection in Spring

In this article, we will be exploring XML-based Injection in Spring. and also creating a sample course management system using ApplicationContext that manages the complete lifecycle of a bean from its creation to destruction in a seamless manner.

XML-based injection

How Spring uses XML configuration files:

Common places for XML configuration (beans, context, web):

Step-by-Step Implementation of XML-Based Injection in Spring

Step 1: Create a new Spring Project



Project Structure:

Below is the structure of the Project.



Step 2: Model class [Course.java]




package com.example.demo;
  
public class Course {
    private int id;
    private String course_name;
    private double course_fees;
    private double course_duration;
  
    public Course()
    {
        super();
        // TODO Auto-generated constructor stub
    }
  
    public Course(int id, String course_name,
                  double course_fees,
                  double course_duration)
    {
        super();
        this.id = id;
        this.course_name = course_name;
        this.course_fees = course_fees;
        this.course_duration = course_duration;
    }
  
    public int getId() {
      return id; 
    }
  
    public void setId(int id) {
      this.id = id; 
    }
  
    public String getCourse_name() {
      return course_name; 
    }
  
    public void setCourse_name(String course_name)
    {
        this.course_name = course_name;
    }
  
    public double getCourse_fees() {
      return course_fees; 
    }
  
    public void setCourse_fees(double course_fees)
    {
        this.course_fees = course_fees;
    }
  
    public double getCourse_duration()
    {
        return course_duration;
    }
  
    public void setCourse_duration(double course_duration)
    {
        this.course_duration = course_duration;
    }
}

The above Course class defines the properties of a course like id, name, fees and duration. Getter and setter methods are provided to access and modify these properties.

Step 3: Create a Service class to manage all courses [CourseService.java]




package com.example.demo;
  
public class CourseService 
{
    private Course course;
  
    public void setCourse(Course course)
    {
        this.course = course;
    }
    public void addNewCourse(Course course)
    {
        // logic of add new course
    }
}

Step 4: Configuration file for Spring beans [applicationContext.xml]




<?xml version="1.0" encoding="UTF-8"?>
  
    <bean id="course" class="com.example.demo.Course"/>
  
    <bean id="courseService" class="com.example.demo.CourseService">
          
        <property name="course" ref="course"/>
    </bean>
  
</beans>

The above XML configuration file defines Spring beans for the Course and CourseService classes, injecting the Course dependency into the CourseService bean.

Step 5: Main Class to run the code




package com.example.demo;
  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
@SpringBootApplication
public class XmlBasedApplication 
{
    public static void main(String[] args)
    {
        ApplicationContext context
            = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        CourseService courseService
            = (CourseService)context.getBean(
                "courseService");
  
        // Adding New Course
        Course course = new Course();
        course.setId(1);
        course.setCourse_name("Java");
        course.setCourse_duration(7);
        course.setCourse_fees(40000);
  
        courseService.addNewCourse(course);
        System.out.println("Course Name:"
                           + course.getCourse_name());
        System.out.println("Course Duration:"
                           + course.getCourse_duration()
                           + "Months");
        System.out.println("Course Fees:"
                           + course.getCourse_fees());
    }
}

Output:

For better understanding refer to the below output video.


Article Tags :