Showing posts with label Inversion of control. Show all posts
Showing posts with label Inversion of control. Show all posts

Wednesday, August 22, 2012

Dependency Injection(IOC) in Spring

This is 2 of 13 parts of tutorial series

Tutorial Content:



Part-1:Introduction to spring framework
Part-2:Dependency injection(ioc) in spring
Part-3:Spring hello world example in eclipse
Part-4:Dependency injection via setter method in spring
Part-5:Dependency injection via constructor in spring
Part-6:Spring Bean scopes with examples
Part-7:Initializing collections in spring
Part-8:Beans Autowiring in spring
Part-9:Inheritance in Spring
Part-10:Spring ApplicationContext
Part-11:Spring lifetime callbacks
Part-12:BeanPostProcessors in Spring
Part-13:Annotation based Configuration in spring

The basic concept of the dependency injection (also known as Inversion of Control pattern) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.

i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects. 


Example:

 Lets we have two classes-Car and Engine.Car has a object of Engine.

Normal way:
There are many ways to instantiate a object. A simple and common way is with new operator.
so here Car class contain object of Engine and we have it instantiated using new operator.

Without DI


With help of Dependency Injection:
Now we outsource instantiation and supply job of instantiating to third party.Car needs object of Engine to operate but it outsources that job to some third party. The designated third party, decides the moment of instantiation and the type to use to create the instance. The dependency between class Car and class Engine is injected by a third party. Whole of this agreement involves some configuration information too. This whole process is called dependency injection.



With DI

How this whole dependency injection works,we will see it in further posts.

Benifits of Dependency Injection in Spring:

  • Ensures configuration and uses of services are separate.
  • Can switch implementations by just changing configuration.
  • Enhances Testability as mock dependencies can be injected.
  • Dependencies can be easily identified.
  • No need to read code to see what dependencies your code talks to.  

Types of Dependency Injection:



  • Inteface Injection: In interface-based dependency injection, we will have an interface and on implementing it we will get the instance injected.
We will see above injection type in further posts.
In next post we will see spring hello world program in spring.

Tuesday, August 21, 2012

Introduction to Spring Framework

Target Audience

This tutorial is designed for Java programmers who need to understand the Spring 3 framework and its application.

Prerequisites:

Before proceeding with this tutorial you should have a good understanding of the Java programming language.
This is 1 of 13 parts of tutorial series

Tutorial Content:


Part-1:Introduction to spring framework
Part-2:Dependency injection(ioc) in spring
Part-3:Spring hello world example in eclipse
Part-4:Dependency injection via setter method in spring
Part-5:Dependency injection via constructor in spring
Part-6:Spring Bean scopes with examples
Part-7:Initializing collections in spring
Part-8:Beans Autowiring in spring
Part-9:Inheritance in Spring
Part-10:Spring ApplicationContext
Part-11:Spring lifetime callbacks
Part-12:BeanPostProcessors in Spring
Part-13:Annotation based Configuration in spring

Introduction: 

Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development 

Features:

  • The basic concept of the Dependency Injection or Inversion of Control is that, programmer do not need to create the objects, instead just describe how it should be created. No need to directly connect your components and services together in program, instead just describe which services are needed by which components in a configuration file/xml file. The Spring IOC container is then responsible for binding it all up.
  • Spring contains and manages the life cycle and configuration of application objects.
  • Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.
  • Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring's transaction support is not tied to J2EE environments and it can be also used in container less environments.
  • The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS

 Architecture:

Spring is well-organized architecture consisting  of seven modules. Modules in the Spring framework are:
  1. Spring AOP:

    One of the key components of Spring is the AOP framework. AOP is used in Spring:
    • To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management, which builds on Spring's transaction abstraction.
    • To allow users to implement custom aspects, complementing their use of OOP with AOP
  2. Spring ORM:

    The ORM package is related to the database access. It provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis.
      
  3. Spring Web:

    The Spring Web module is part of Spring?s web application development stack, which includes Spring MVC.
     
  4. Spring DAO:

    The DAO (Data Access Object) support in Spring is primarily for standardizing the data access work using the technologies like JDBC, Hibernate or JDO.
     
  5. Spring Context:

    This package builds on the beans package to add support for message sources and for the Observer design pattern, and the ability for application objects to obtain resources using a consistent API.
      
  6. Spring Web MVC:

    This is the Module which provides the MVC implementations for the web applications.
      
  7. Spring Core:

    The Core package is the most import component of the Spring Framework.
    This component provides the Dependency Injection features. The BeanFactory  provides a factory pattern which separates the dependencies like initialization, creation and access of the objects from your actual program logic.

Spring Framework Architecture

In next post,we will see dependency injection(ioc) in spring.