mantri
mantri

Reputation: 3191

Which type of Dependency Injection it is when we have @Autowire on a class field?

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;
    ...
}

Upvotes: 3

Views: 175

Answers (2)

Afridi
Afridi

Reputation: 6932

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

Upvotes: 1

Sergey Prokofiev
Sergey Prokofiev

Reputation: 1885

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.

Upvotes: 2

Related Questions