Spring-1

SyntaxHighlighter.all();

Spring is framework that is based on two main concepts :-

1) IOC – Inversion Of Control

2) AOP – Aspect Oriented Programming

IOC is a design pattern that transfers the control of creating objects and satisfying their dependencies to a third party called IOC Container.

This transfer of control has following advantages over conventional methods :-

1) Objects are decoupled which results in maintenance simplification.

2) Application developers need to focus only on usage of object i.e they are not concerned how the objects are created and how the resources required to objects are provided.

Let Object A need to use Object B for performing some operation and Object B requires Object C to perform its functions.

IOC Container        Object A              Object B              Object C

1.0 –  Object A asks Object B  from IOC Container

1.1 – Object B is created by IOC Container.

1.2  – Object C is created by IOC Container to satisfy the dependency of Object B

1.3 – Reference of Object C is provided to Object B.

1.4 – Reference of Object B is given to Object A.

Implementation of IOC is provided by different frameworks using following 2 means :-

1) Dependency Injection :- In case of Dependency Injection dependency of object are satisfied implicitly before the object is used.

2) Dependency Lookup – In case of Dependency Lookup dependencies are satisfied when asked i.e. an object need to ask resource required by it to perform its operation.

IOC Container

It is a runtime environment that creates object satisfy their dependencies manages their lifecycle on behalf of the application developer.

Spring Framework contains various modules which can be used independently or can be used collectively.

Core :- Core Module provides the basic implementation of an IOC container.

Context :- Context Module is built over Core and provides advanced implementation of IOC container.

AOP :- This module facilitate Aspect Oriented Programming.

Tx :- This module provides the facility of transaction management.

JDBC :- This module simplifies data connectivity by providing JDBC template which automates common operations.

ORM :- This module provides the facility of integrating Spring application to an ORM framework such as Hibernate.

Web :- This module is built over context and automates common operations such as transferring of request parameters to domain objects, file uploading, downloading etc.

Web MVC :- This module provides Spring implementation of MVC design pattern for Web application.

Web Integration :- This module facilitate integration of Spring with third party MVC implementation such as Struts.

Enterprise Integration :- This module facilitate integration of Spring Framework with enterprise API’s such as JMS, Java Mail, EJB etc.

Testing :- This module simplifies unit and integration testing.

Inversion Of Control (IOC) 

Spring Framework exposes an interface named BeanFactory to application developers. This interface describes the functionality of a basic IOC container.

Various implementation of this interface are provided by the framework. This interface contain multiple methods. Most commonly used among them are :-

getBean() :- This method is used by an application to obtain a bean from IOC container.

public Object getBean(String beanName)

In Spring a bean represents an object that is created and managed by the IOC Container.

IOC Container requires information to manage beans. This information can be provided through XML or annotations.

At the time of execution configuration information provided to IOC container through XML or annotation is maintained in a Resource object.

Resource is an interface exposed by the framework. Multiple implementation of this interface are provided by the Framework. A Resource object is required by BeanFactory to create and manage beans.

Java Application    XML Config File     Resource   Bean Factory   Bean

1.0 – Resource Object is created by Java App

1.1 – Configuration Info is read and stored in object form by Resource object.

1.2 – Bean Factory is created and reference of Resource object is provided by Java App.

1.3 – Java App ask bean from Bean Factory using getBean()

1.4 – Information required to create bean is obtained by BeanFactory from Resource.

1.5 – Bean is instantiated by Bean Factory and its dependencies are satisfied.

1.6 – Reference of Bean is returned by Bean Factory to Java App.

1.7 – Bean is used by Java App.

Here is small code snippet example

Number.java

package mypack;

public interface Number

{
    public Number add(Number);

    public void display();
}

applicationContext.xml

<bean-id = "num2" class = "mypack.Complex">

<constructor-arg value = "2"/>

<constructor-arg value = "5"/>

</bean>
public class WithSpring

{

    public static void main(String args[])

    {

        Resource r = new ClassPathResource("applicationContext.xml");

        BeanFactory f = new XMLBeanFactor(r);

        System.out.pritln("Obtaining bean from IOC container");

        Number n1 = (Number)f.getBean("num1");

        Number n2 = (Number)f.getBean("num2");

        Number n3 = n1.add(n2);

        n3.display();

    }
}

IOC container can create an object directly using new() operator or it can use the factory method of the same class or it can use the factory class for instantiating the bean.

In case of Factory, object of a class can be created using following 3 methods :-

1) static factory method of the class

2) static factory method of different class.

3) non-static factory method of different class.

Ex :-

class A

{

    private A()

    {}

    public static A getA()
    {
        return new A();
    }
}

Object of A can be created as –

A x = A.getA();

Following mapping is required for this –

<bean id = “a” class = “A” factory.method = “getA”/>

A x = (A)f.getBean(“a”);

IOC container shall invoke getA() factory method of A.

When factory method is in class B:-

class B

{

    public static A getA()

    {
        return new A();

    }
}

A x = B.getA();

<bean id=”a” class=”B” factory method=”getA”/>

A x = (A)f.getBean(“a”);

IOC container will invoke factory method getA() of class B.

IOC Container uses Injection method for satisfying the dependency of objects. Following 2 approach of injection are supported by IOC Container –

1) Constructor Injection                 2) Setter Injection

In case of constructor injection dependency of object is satisfied by the IOC container by passing the reference of dependent objects or values as arguments to constructors.

About neer1304

An INTJ Curious Geek
This entry was posted in Framework, Java and tagged . Bookmark the permalink.

Leave a comment