Spring Framework Xml Approach

Spring Framework Xml Approach

·

3 min read

first let's see setter injection using core java

code:

This is the project structure

EngineDepedent.java

package com.linkdin.dependent;

public class EngineDependent {

    static {
        System.out.println("EngineDependent.class file is loading...");
    }

    public EngineDependent() {
        System.out.println("EngineDependent object is created...");
    }
}

Car.java

package com.linkdin.Target;

import com.linkdin.dependent.EngineDependent;

public class Car {
    static {
        System.out.println("Car.class file is loading...");
    }
    public Car() {
        System.out.println("Car object is created...");
    }
    private EngineDependent engine;

    public void setEngine(EngineDependent engine) {
        this.engine=engine;
        System.out.println("setter method is called and engine object is injected");
    }

    public void start() {
        if(engine!=null) {
            System.out.println("engine is started,so that is reason car started\nengine object is injected");
        }else {
            System.out.println("engine is not started because engine object is not injected,\nso car will not start");
        }
    }
}

MainClass.java

package com.linkdin.Main;

import com.linkdin.Target.Car;
import com.linkdin.dependent.EngineDependent;

public class MainClass {

    public static void main(String[] args) {
        //creating dependent object(engine)
        EngineDependent engine=new EngineDependent();
        //creating target object(car)
        Car car=new Car();
        //performing setter injection
        car.setEngine(engine);
        //starting the car using start()
        car.start();
    }

}

run the main method class to get output

XmlApproach-setterinjection

we need to add spring framework jar in ide to this approach,if you don't know check out this blog click here

if you can see we dont need this below content in main method if you are using ioc container.

    //creating dependent object(engine)
        EngineDependent engine=new EngineDependent();
        //creating target object(car)
        Car car=new Car();
        //performing setter injection
        car.setEngine(engine);

we just need to call target class method which is car.start()

project structure of setter injection xml approach

EngineDepedent class and Car class is same as we done above,the application.xml is add and MainClass main method is modified.

MainClass.java

Code:

package com.linkdin.Main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.linkdin.Target.Car;

public class MainClass {

    public static void main(String[] args) {
    /*step 1*/    ApplicationContext ac =new ClassPathXmlApplicationContext("com\\linkdin\\cfg\\application.xml");

                //want to call start method in Car class,assuming remaining duty will be done by ioc container
    /*step 2*/    Car car = ac.getBean(Car.class);

    /*step 3*/    car.start();
    }

}

step 1:

step 2:

Implementation of ApplicationContext object performs is basically a container which performs eager loading.

Eager loading means whenever the container is started, by using bean information in application.xml file,the ioc continer create all objects (or) beans and keep it ready in the container.

whenever getBean method called by providing class name or id in the parameter (or) argument,the container (or) Implementation class of application context object, give required object, so that we can use the object.

Step 3:

calling car.start() method which is target object method.

application.xml

application.xml is an xml file where we provide bean information for ioc container.

code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- bean definitions here -->

    <!-- dependent bean or object-->
    <bean id="carengine" class="com.linkdin.dependent.EngineDependent"/>

    <!-- target bean or object-->
    <bean id="car" class="com.linkdin.Target.Car">
        <!-- performing setter injection adding dependent object-->
        <property name="engine" ref="carengine"/>
    </bean>

</beans>

<beans spring framework namespace>bean information</beans>

bean information:

we will learn xml approch for learning purpose,as we move to java code configuration and Spring boot, we will not use xml approach,we will use java class for providing bean information. understanding this concept is not a waste, it will help while writing bean information in java class.