3

One way is the DI using setter methods. Other way is using constructor.

I am just curious to know which type of DI the following way is :

public class Test {

    @Autowired
    TestService service;
    ...
}
mantri
  • 3,031
  • 4
  • 16
  • 19

2 Answers2

2

That is "field injection". Generally you can choose from 3 types:

  • Field injection
  • Constructor injection
  • Setter injection

Each of them has advantages and disadvantages. During the testing field injection is normal practice.

Sergey Prokofiev
  • 1,815
  • 1
  • 12
  • 21
1

In case of @Autowiring in a class like:

public class Test {

    @Autowired
    TestService service;
    ...
}

Autowire your object on using reflection, hence no need for Setter methods

Check this post: How does Spring @Autowired work

Community
  • 1
  • 1
Afridi
  • 6,753
  • 2
  • 18
  • 27