Spring

What is Spring?

The term Spring refer to a bunch of tools and frameworks that Spring project provides. Below is their listing:

  • Spring Core Framework based on inversion of control (IOC)
  • Spring MVC framework
  • Spring AOP (Aspect Oriented Programming).
  • Spring Web flow

In this article, I would be discussing Spring Core Framework only. In the rest of the article, I would be referring the term Spring for Spring core framework.

Spring Core framework is based on Inversion Of control (IOC) principle. So lets first explore IOC.

What is Inversion of Control (IOC)?

IOC is a principle that provides a simpler mechanism for provisioning components dependencies and managing these dependencies throughout their life cycles.

Difference b/w Inversion Of Control (IOC) and Dependency Injection(DI)

People most often used the term DI and IOC interchangeably thinking they are same but this is not the case. DI is a subtype of IOC.

IOC can be divided in following two subtypes:

  • Dependency lookup
  • Dependency Injection (DI)

Dependency Lookup

Dependency lookup is a traditional approach that java programmers have used for looking up EJB using JNDI. e.g see the following code for invoking EJB method sayHello() by looking EJB through JNDI in the code:

public Class HelloClient{

public void sayHello(){

Context ctx = new InitialContext();

Hello hello = (Hello)ctx.lookup(“examples.session.stateless.Hello”);

hello.sayHello();

}

}

In dependency lookup, component must acquire a reference to a dependency, like we get the reference of Hello EJB in above example.

Dependency Injection

DI is a newer, less well established approach that, although apparently counterintuitive at first, is actually much more flexible and usable than dependency lookup. EJB 3.0 introduces DI through its annotation e.g @EJB to inject EJB.

public Class HelloClient{

@EJB

private examples.session.stateless.Hello hello;

public void sayHello(){

hello.sayHello();

}

}

In above example EJB class examples.session.stateless.Hello is instantiated and injected by the container in the code reference hello.

When we use EJB in Spring then we do not need to do lookup for the EJB. Spring has the capability to automatically inject it in the code.

Types of Dependency Injection

Constructor DI

In this type of DI dependencies are provided to it in its constructor. The component declares a constructor or a set of constructor, taking as argument its dependencies. The IOC container passes the dependencies to the component when it instantiate it.e.g

Public class Message{

MessageService msgService;

Message(MessageService msgService){

this.msgService=msgService;

}

public void sendMessage(String msg){

msgService.sendMessage(msg);

}

}

In the above example IOC container will bee passing MessageService class dependency in the constructor when instantiate its object.

Setter DI

In setter DI, the IoC container injects a component’s dependencies into the component via JavaBean

style setter methods. A component’s setters expose the set of the dependencies the IoC container

can manage.

Public class Message{

MessageService msgService;

public void setMessageService(MessageService msgService){

this.msgService=msgService;

}

public void sendMessage(String msg){

msgService.sendMessage(msg);

}

}

Constructor DI Vs Setter DI

Constructor injection is particularly useful when we absolutely must have an instance of the dependency class before our component is used. Many containers including Spring provide a mechanism for ensuring that all dependencies are defined when we use setter injection, but by using constructor injection, we assert the requirement for the dependency in a container-agnostic manner.

Setter injection is useful in a variety of different cases. If we want the component to expose its dependencies to the container but provide its own defaults, setter injection is usually the best way to accomplish this.

How DI works in Spring?

The core of Springs’s DI container is the BeanFactory. A BeanFactory is responsible for managing components and their dependencies

Applications interact with the Spring DI container via the BeanFactory interface. There are two implementation of BeanFactory that comes with Spring.

  • DefaultListableBeanFactory

  • XMLBeanFactory

DefaultListableBeanFactory

This factory provides ability to read BeanDefinition information from a property file using the PropertiesBeanDefinitionReader as shown below:

Properties File:

car.(class)=com.folio3.Car

engine.(class)=com.folio3.Engine

car.engine(ref)=engine

code

Public class Engine{

public void start(){

System.out.println(“engine started”);

}

}

Public class Car{

private Engine engine;

public void start(){

engine.start();

}

}

public class BeanFactoryDemo {

public static void main(String[] args) {

DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(bf);

reader.loadBeanDefinitions(new ClassPathResource( “/META-INF/spring/beanfactorydemo-context.properties”));

Car car= (Car) bf.getBean(“car”);

System.out.println(“Car is starting ” + car.start());

}

}

XMLBeanFactory

This factory provides ability to read BeanDefinition information from a XML file using the XmlBeanDefinitionReader as shown below

XML File:

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd”&gt;

<bean id=”car” class=”com.folio3.Car”>

<property name=”engine”>

<bean class=”com.folio3.Engine”/>

</property>

</bean>

</beans>

Code:

Public class Engine{

public void start(){

System.out.println(“engine started”);

}

}

Public class Car{

private Engine engine;

public void start(){

engine.start();

}

}

public class XmlBeanFactoryDemo {

public static void main(String[] args) {

XmlBeanFactory bf = new XmlBeanFactory(

new ClassPathResource(“/META-INF/spring/beanfactorydemocontext.xml”));

Car car = (Car) bf.getBean(“car”);

System.out.println(“Car is starting” + car.start());

}

}

Advertisements

2 thoughts on “Spring

  1. For IOC and DI.
    IOC is just an abstract architectural principle or just an architectural concept. and to achieve this DI is one the many design pattern including service lookup or dependency lookup. The difference between any framework and an API is the inversion of control.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s