Java – What type of dependency injection is it when we use @Autowire on a class field?

What type of dependency injection is it when we use @Autowire on a class field?… here is a solution to the problem.

What type of dependency injection is it when we use @Autowire on a class field?

One way is for DI to use the setter method. Another way is to use constructor.

I was just wondering what type of DI the following way is:

public class Test {

@Autowired
    TestService service;
    ...
}

Solution

That is, “field injection”. Usually you can choose from 3 types:

  • Field injection
  • Constructor injection
  • Setter injection (inject).

They each have their own advantages and disadvantages. Injecting in the test field is normal practice.

Related Problems and Solutions