1、 settter方法注入 以@Autowired注解为例,即把@Autowired注解标记在目标bean的引用bean的setter方法上。
kotlin
@RestController
@RequestMapping("/abc")
public class AbcController {
private AbcService abcService;
@Autowired
public void setAbcService(AbcService abcService) {
this.abcService = abcService;
}
@GetMapping("/rateLimit")
public String rateLimit(){
System.out.println("start");
abcService.printABC();
return "abc";
}
}
2.构造器注入
kotlin
@RestController
@RequestMapping("/abc")
public class AbcController {
private AbcService abcService;
public AbcController(AbcService abcService) {
this.abcService = abcService;
}
@GetMapping("/rateLimit")
public String rateLimit(){
System.out.println("start");
abcService.printABC();
return "abc";
}
}
3.变量(filed) 注入
kotlin
@RestController
@RequestMapping("/abc")
public class AbcController {
@Autowired
private AbcService abcService;
@GetMapping("/rateLimit")
public String rateLimit(){
System.out.println("start");
abcService.printABC();
return "abc";
}
}

