Spring依賴注入的三種方式詳解
更新時間:2023年11月23日 08:25:55 作者:源碼超級聯(lián)盟
這篇文章主要給大家介紹了三種Spring依賴注入的方式,?settter方法注入,構(gòu)造器注入以及變量(filed)?注入這三種方式,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
1、 settter方法注入 以@Autowired注解為例,即把@Autowired注解標(biāo)記在目標(biāo)bean的引用bean的setter方法上。
@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.構(gòu)造器注入
@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) 注入
@RestController
@RequestMapping("/abc")
public class AbcController {
@Autowired
private AbcService abcService;
@GetMapping("/rateLimit")
public String rateLimit(){
System.out.println("start");
abcService.printABC();
return "abc";
}
}


以上就是Spring依賴注入的三種方式詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring依賴注入的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot登陸頁面圖片驗證碼簡單的web項目實現(xiàn)
這篇文章主要介紹了springboot登陸頁面圖片驗證碼簡單的web項目實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
一行命令同時修改maven項目中多個module的版本號的方法
這篇文章主要介紹了一行命令同時修改maven項目中多個module的版本號的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
spring boot jpa寫原生sql報Cannot resolve table錯誤解決方法
在本篇文章里小編給大家整理的是關(guān)于spring boot jpa寫原生sql報Cannot resolve table錯誤的解決方法,需要的朋友學(xué)習(xí)下。2019-11-11
5分鐘讓你快速掌握java8 stream常用開發(fā)技巧
這篇文章主要給大家介紹了關(guān)于java8 stream常用開發(fā)技巧的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

