Spring IoC (Dependency injection ) Tutorial - Part 2

Inversion of Control :
  • Dependency injection 
    • Beans define their dependencies through constructor arguments or properties.
    • The container provides the injection at runtime.
  • Decouples object creators and locators from application logic 
  • Easy to maintain and reuse.
  • Testing is easier.

Inversion of Control has already been referred to as Dependency Injection. The basic principle is that beans define their dependencies (i.e. the other objects they work with) only through constructor arguments or properties. Then, it is the job of the container to actually inject those dependencies when it creates the bean. This is fundamentally the inverse (hence the name Inversion of Control) of the bean instantiating or locating its dependencies on its own using direct construction of classes, or something like the Service Locator pattern. While we will not elaborate too much on the advantages of Dependency Injection, it becomes evident upon usage that code gets much cleaner and reaching a higher grade of decoupling is much easier when beans do not look up their dependencies, but are provided them, and additionally do not even know where the dependencies are located and of what actual type they are.
As touched on in the previous paragraph, Inversion of Control/Dependency Injection exists in two major variants:

setter-based dependency injection is realized by calling setters on your beans after invoking a no-argument constructor to instantiate your bean. Beans defined in the BeanFactory that use setter-based dependency injection are true JavaBeans. Spring generally advocates usage of setter-based dependency injection, since a large number of constructor arguments can get unwieldy, especially when some properties are optional.

constructor-based dependency injection is realized by invoking a constructor with a number of arguments, each representing a collaborator or property. Although Spring generally advocates usage of setter-based dependency injection as much as possible, it does fully support the constructor-based approach as well, since you may wish to use it with pre-existing beans which provide only multi-argument constructors, and no setters. Additionally, for simpler beans, some people prefer the constructor approach as a means of ensuring beans can not be constructed in an invalid state.

Non-IoC / Dependency Injection :

public class OrderServiceImpl implements IOrderService 
public Order saveOrder(Order order) throws OrderException
 // 1. Create a Session/Connection object
 // 2. Start a transaction
 // 3. Lookup and invoke one of the methods in a
  // DAO and pass the Session/Connection object.
 // 4. Commit transaction
 }catch(Exception e){
 // handle e, rollback transaction, //cleanup, // throw e
 //Release resources and handle more exceptions

IoC / Dependency Injection :

Program to interfaces for your bean dependencies

public class OrderSpringService implements IOrderService {
IOrderDAO orderDAO;
public Order saveOrder(Order order) throws OrderException{
 // perform some business logic…
 return orderDAO.saveNewOrder(order);

public void setOrderDAO(IOrderDAO orderDAO) {
 this.orderDAO = orderDAO;

The DAO would be injected with a Session/Connection object.

Spring Bean Definition :
  • The bean class is the actual implementation of the bean being described by the BeanFactory.
  • Bean examples – DAO, DataSource, Transaction Manager, Persistence Managers, Service objects, etc
  • Spring config contains implementation classes while your code should program to interfaces.
  • Bean behaviors include:
    • Singleton or prototype
    • Autowiring
    • Initialization and destruction methods 
      • init-method
      • destroy-method
  • Beans can be configured to have property values set. 
    • Can read simple values, collections, maps, references to other beans, etc.
Key attributes :
  • class (required): fully qualified java class name
  • id: the unique identifier for this bean
  • configuration: (singleton, init-method, etc.)
  • constructor-arg: arguments to pass to the constructor at creation time
  • property: arguments to pass to the bean setters at creation time
  • Collaborators: other beans needed in this bean (a.k.a dependencies), specified in property or constructor-arg

    Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

    Make sure to activate your subscription by clicking on the activation link sent to your email