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.
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;
}
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.