Spring IoC tutorial – part 1

The Spring Framework was released in October 2002 as an open source framework.
The main target of the framework was to act as inversion of a control container.
Inversione of Control (Ioc) and Dependency Injection (DI) are strictly related: It is all about removing dependencies from your code. These terms are often used interchangeably.
You normally would do this:

public class Reservation {
    private RentedCar car;
    public Reservation() {
        this.car = new RentedCar();
    }
}

In the above code the Reservation instantiation depends on RentedCar creation:
Reservation myReservation=new Reservation();
becase in the Reservation creation you instatiate a RentedCar object.
With IoC design pattern instead you define the reservation class like this:
public class Reservation {
    private RentedCar car;
    public Reservation(RentedCar car) {
        this.car = car;
    }
}

And then you inject a RentedCar in a Reservation object:
RentedCar car=new RentedCar();
Reservation myReservation=new Reservation(car);

The IoC pattern has been applied here. Inversion of control inverts the flow of control of the program. Instead of the callee controlling the flow of control(while creating dependencies), the caller controls the flow of control of the program.
Dependency injection generally means passing an object on which method depends, as a parameter to a method, rather than having the method create the dependent object.
So what we did in the above example? We created a RentedCar object and passed it to the constructor, so we injected the RentedCar object into the Reservation object.
How can spring facilitate all this process? Supposed we have got the following class defined:
package jtrainer.education.tutorial.spring.ioc;

public class Reservation {
    RentedCar rentedCar;
    public Reservation() {
        super();
    }
    public Reservation(RentedCar rentedCar) {
        super();
        this.rentedCar = rentedCar;
    }
    public RentedCar getRentedCar() {
        return rentedCar;
    }
    public void setRentedCar(RentedCar rentedCar) {
        this.rentedCar = rentedCar;
    }
}

public class RentedCar {
    String model="FIAT500";
    
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
}

The IoC pattern can be implemented in a xml configuration file which spring will read:
<?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-3.2.xsd">
    <bean id="reservation" class ="jtrainer.education.tutorial.spring.ioc.Reservation">
        <constructor-arg ref ="rentedCar"/>
    </bean >
 
     <bean id = "rentedCar" class ="jtrainer.education.tutorial.spring.ioc.RentedCar"/> 
</beans>

This configuration file will tell spring framework that the bean with id “rentedCar” must be injected in the Reservation class constructor with RentedCar car argument. This injection is called “by constructor”
We can test this injection using the following class (supposed you put the previuos xml file in src/main/resources)
package jtrainer.education.tutorial.spring.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainIoC_Constructor {
    
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-context-ioc-constructor.xml");
        Reservation myReservation=ctx.getBean("reservation", Reservation.class);
        System.out.println(myReservation.getRentedCar().getModel());
    }
}

Which will output “FIAT500”
The same IoC pattern can be implemented with the following configuration 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean id="reservation" class ="jtrainer.education.tutorial.spring.ioc.Reservation">
        <property name="rentedCar" ref="rentedCar"/>
    </bean >
 
     <bean id = "rentedCar" class ="jtrainer.education.tutorial.spring.ioc.RentedCar"/>
 </beans>

This configuration file will tell spring framework that the bean with id “rentedCar” must be injected in the Reservation class trough the method “setRentedCar” with RentedCar car argument. This injection is called “by setter”.
We can test this injection using the following class:
package jtrainer.education.tutorial.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainIoC_Setter {

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-context-ioc-setter.xml");
        Reservation myReservation=ctx.getBean("reservation", Reservation.class);
        System.out.println(myReservation.getRentedCar().getModel());
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *