What type of dependency injection when we have @Autowire on a class field?

One way is DI using methods setter

. Another way is using constructor

.

I'm just curious to know what type of DI is next:

public class Test {

    @Autowired
    TestService service;
    ...
}

      

+3


source to share


2 answers


This is a "field injection". You can usually choose one of three types:

  • Field injection
  • Constructor input
  • Installation node


Each has its own advantages and disadvantages. During testing, field injection is common practice.

+2


source


In the case of @Autowiring in a class, for example:

public class Test {

    @Autowired
    TestService service;
    ...
}

      



Free your object from using reflection hence no need for Setter methods

Check this post: How Spring @Autowired Works

+1


source







All Articles