Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Dependency Injection

The basic concept of the Inversion of Control pattern (also known as dependency injection) 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.

Dependency Injection is a way to let the framework or the container works out the complexities of bean instantiation, initialization and sequencing and supplies the bean references to the clients as required. In spring, IOC has a responsibility of doing DI.


Benefit of IOC(Dependency Injection)

Benefits of IOC (Dependency Injection) are as follows:

  • Loose coupling: We do not need to write codes to use the factory method pattern to manage the beans, instead the container manages the bean life cycle. So the application code is free from managing the beans. As a result of this the amount of code is reduced and we can concentrate on the business logic.
     
  • Better Design: It promotes to "program to an interface not an implementation".
     
  • Testability: Application code becomes easier to test using mock objects instead of actual objects.
     
  • Ease of development: It becomes easier to add, remove or alter the services just changing the configuration.
     

Types of IOC (Dependency Injection)

There are two types of Dependency Injection

  • Constructo Injection
    Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments,
    each representing a dependency on other class.
     
  • Setter Injection
    Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument
    constructor or no-argument static factory method to instantiate your bean.
Example of Constructor Injection

Example of Setter Injection


     << Previous
Next >>