作者goldberg123 (SmallHanWinchest)
看板java
标题[问题] autowired问题一问
时间Sat Oct 8 01:35:46 2016
今天我在我的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/cn.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