java 板


LINE

今天我在我的config中autowired幾支class進來 像是這樣 @Autowired private A a; @Autowired private B b; 然後再set某些值進去 public A aSet(){ a.id = 'hello'; return a; } public B bSet(){ b.id = 'world'; return b; } } 然後run server時候會報錯 於是我改成以下這樣 @Autowired private A a; private A a; @Autowired @qulifier("aSet") private A aSet; @Autowired private B b; @Autowired @qulifier("bSet") private B bSet; 然後再set某些值進去 @bean public A aSet(){ public A aSet(){ a.id = 'hello'; return a; } @bean public B aSet(){ b.id = 'world'; return b; } 然後再丟到Map裡頭例如 public Map<String,Test> test(){ public Map<String,Test> test(){ Map<String,Test> mapTest = new ArrayList<String,Test>(); mapTest.put('1',aSet); mapTest.put('2',bSet); return mapTest; } 雖然run server可以過不會報錯 但是我map中的a和b是空值,沒有被set到hello和world; 後來我就印log發現a根本是null b也是null 根本沒拿記憶體位置 後來我就印log發現a根本是null b也是null 根本沒拿記憶體位置 請問是不能這樣set值嗎 還是我autowired錯了 這問題搞了我好久 網路上也有去找答案 但好像沒人用過這種寫法 所以都找不到問題點 煩請各位高手指點 補充完整的程式碼 @Configure public class someConfig { @Autowired private A a; @Autowired @qulifier("aSet") private A aSet; @Autowired @Autowired private B b; @Autowired @qulifier("bSet") private B bSet; @Bean public Map<String,ITest> test(){ Map<String,Test> mapTest = new ArrayList<String,Test>(); mapTest.put('1',a); System.out.println(a) <-- 此時會印null 沒有拿到實體 mapTest.put('2',b); <-- 此時會印null 沒有拿到實體 return mapTest; } @bean public A aSet(){ a.id = 'hello'; return a; } @bean public B aSet(){ b.id = 'world'; return b; } } } --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.185.18.144
※ 文章網址: https://webptt.com/m.aspx?n=bbs/java/M.1475861750.A.CD5.html
1F:→ qrtt1: 要完整可以跑的程式啊,不知你在講什麼東西。10/08 09:28
※ 編輯: goldberg123 (111.185.18.144), 10/08/2016 11:26:53 ※ 編輯: goldberg123 (111.185.18.144), 10/08/2016 12:12:49 ※ 編輯: goldberg123 (111.185.18.144), 10/08/2016 12:25:48
2F:→ kentyeh: 從程式碼來看,我覺得你最好先去看文件或找本書好好看一10/08 14:12
3F:→ kentyeh: 一下,你可以把@Configure看成是@Bean的來源產生器,而10/08 14:12
4F:→ kentyeh: @Autowired是注入人家產生好@Bean的接收者,先問一下 a與b10/08 14:13
5F:→ kentyeh: 變數的注入來源在那裡?至於aSet()與bSet()完全不贊同這種10/08 14:13
6F:→ kentyeh: 寫法,@Bean是用來產生注入來源,不是讓你用來修一個既有10/08 14:14
7F:→ kentyeh: Bean的內容值,若要讓Bean有初值,你可以讓這個Bean的10/08 14:14
8F:→ kentyeh: Bean的內容值,若要讓Bean有初值,你可以讓這個Bean的10/08 14:14
9F:→ kentyeh: class implement InitializingBean或是這樣寫10/08 14:15
10F:→ kentyeh: @Bean10/08 14:15
11F:→ kentyeh: public A aSet(){10/08 14:15
12F:→ kentyeh: A a = new A();10/08 14:15
13F:→ kentyeh: a.id="hello";10/08 14:15
14F:→ kentyeh: return a;10/08 14:15
15F:→ kentyeh: }10/08 14:15
16F:→ kentyeh: }10/08 14:15
17F:→ kentyeh: 不要想用@Bean自已產生然後注入自已而是用上方改寫的aSet10/08 14:16
18F:→ kentyeh: @Bean10/08 14:16
19F:→ kentyeh: public Map test(){10/08 14:16
20F:→ kentyeh: Map mapTest = new ArrayList<>();10/08 14:17
21F:→ kentyeh: mapTest.put('1',aSet()); ....10/08 14:18
22F:→ kentyeh: }10/08 14:18
※ 編輯: goldberg123 (111.185.18.144), 10/08/2016 15:20:41 ※ 編輯: goldberg123 (111.185.18.144), 10/08/2016 15:21:16
23F:→ ssccg: 我覺得你根本沒搞清楚dependency injection在幹麻 10/08 15:25
24F:→ ssccg: 同一個物件裡面自己產生(@Bean)又自己接收來用(@Autowired) 10/08 15:26
25F:→ ssccg: 毫無意義,直接呼叫就好了 10/08 15:26
26F:→ goldberg123: 抱歉~我再去看一次官方文件~謝謝兩位高手提點 10/08 15:41
27F:→ pttworld: 加油。 10/08 15:42
28F:→ ssccg: btw如果test需要a,b兩個dependency 10/08 15:47
29F:→ ssccg: 可以寫成 @Bean public Map test(A a, B b) { ... } 10/08 15:48
30F:→ qrtt1: 另外就是,你寫這個是想要達到什麼目的?看不太懂意圖 10/08 18:17
31F:→ goldberg123: 我想先把bean inject進來,再依照需求對bean裡的屬性 10/09 12:29
32F:→ goldberg123: 做塞值動作,所以我才會先autowired進來再去set值 10/09 12:29
33F:→ goldberg123: 不過好像違背@autowired精神,當初考量到這樣寫是因為 10/09 12:30
34F:→ goldberg123: 是我有一支class需要做成兩個不同的bean 10/09 12:32
35F:→ goldberg123: 就是同一支class只是差在屬性不同 A的bean的id='A' 10/09 12:32
36F:→ goldberg123: B的bean的id='B'這樣 10/09 12:33







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP