Dependency Injection

In Spring framework, Dependency Injection (DI) is nothing but a design pattern which is used to define the object dependencies between each other that means removes dependencies arises in code. It makes our programming code loosely coupled. There are two ways provided by Spring framework to inject dependencies, such as :

  • • Setter Injection
  • • Constructor Injection



Setter Injection

This is the most popular and simple Dependency Injection method. In this injection, the spring container uses setter method in the dependent class for injecting its dependencies. Spring container perform setter injection by reading the information from an external file called spring configuration file. In spring each class is known as spring bean, this bean is no way related with the java bean Spring bean and java bean both are not same because a java bean needs definitely a public default constructor but in spring bean, some time we include default constructor or sometimes we do not.


Constructor Injection

This is another dependency injection method .Here in this type of injection constructor of bean class is used by the container. In configration file, we have to use to inform the IOC container. If both constructor and setter injection applied for same properly in bean class then constructor injection will be overriden by setter injection.


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext