0

I have a rest controller like this :

@Slf4j
@RestController
@RequestMapping(...)
public class MyController {

private MyService service;

public MyController(MyService service){
   this.service = service;
}

And the service class is a component :

@Component
public class MyService{
  ...
}

And when I run the program, the service field is correctly injected. But how is it injected (there is no autowired annotation neither on filed ni on constructor) ?.

I am using SpringBoot 2.0.

hawarden_
  • 1,904
  • 5
  • 28
  • 48
  • 1
    Please refer this https://stackoverflow.com/questions/46523305/spring-inject-without-autowire-annotation/46525216 it might help. – Reeta Wani Apr 01 '20 at 15:56

1 Answers1

2

From Spring 4.3 release. According to the documentation (https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-spring-beans-and-dependency-injection), if a bean has single constructor, @Autowired annotation can be omitted.

If a bean has one constructor, you can omit the @Autowired, as shown in the following example:

@Service
public class DatabaseAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    public DatabaseAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
    }

    // ...

}
backdoor
  • 891
  • 1
  • 6
  • 18