Posts

Showing posts from March, 2011

Dependency Injection using Spring Framework

What is dependency injection? Dependency injection (DI) is the method to create needed dependencies or look them up somehow without doing it in the dependent code. You do not create your objects but describe how they should be created. Then, it is the job of the container (like Spring container) to actually inject those dependencies when it creates the bean. This is fundamentally the inverse of the bean itself being in control of instantiating or locating its dependencies on its own using direct construction of classes. Hence DI is also referred to as Inversion of Control (IoC). Spring supports two types of dependency injection: Setter Dependency Injection: Dependency is provided through setter methods as per Java Bean style. Constructor Dependency Injection: The dependency is injected via constructor arguments. What is a bean? Bean is a typical java bean with a unique id. A bean is simply an object that is instantiated, assembled and otherwise managed by a Spring IoC conta