In Spring in how many ways Dependency Injection is resolved?

Sagar Kudu
1 min readMar 29, 2021
Official — Dependency Injection

Dependency can be done in 3 ways

  1. Setter Injection
  2. Constructor injection.
  3. Field-based injection (using @Autowired)

E.g Student class is dependent on Address class, so after configuration of beans spring will automatically inject Address into Student as per requirement which is done by IOC container.

Setter Injection

→ setter method is to set the values.

Setter injection is also known as property injection.

→ when an IOC container is creating an object, say Address object, it will call the Setter methods to put the values, and as soon as the setter method is called the setter method will have values ready i.e setCity(city) will set the value into the city.

Constructor Injection

→ To set values constructor is used here.

Configuration file

→ in this XML file, we declare the beans and their dependencies.

To declare beans we use tags <beans> </beans>

<beans> represents a number of beans and inside it, we define multiple <bean> and its dependencies.

Field-based Injection

In the case of Field-Based Dependency Injection, we can inject the dependencies by marking them with an @Autowired annotation.

For queries reach out to me at sagar978@gmail.com.

--

--

Sagar Kudu

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Connect https://www.linkedin.com/in/sagarkudu/