我需要一个带有一些内部字段的服务(单例拟合),比如挂起的线程列表(是的,所有内容都写成线程安全的)问题是,如果我使用@autowire
这个 bean,字段似乎是空的。调试我看到代理正确绑定到 CGLIB$CALLBACK_X
具有填充字段的实例(字段正确链接到填充的 bean),但它提供的字段是空的。
以下代码行大致了解我在说什么。
@Service
public class myService{
@Autowired
private Monitor monitor;
public List getSomething(){
return monitor.getList();
}
}
@Service
public class myStatefulService{
//This field will be populated for sure by someone before getSomething() is called
private List list;
public synchronized List getSomething(){
return this.list;
}
//Called by other services that self inject this bean
public synchronized void addToList(Object o){
this.list.add(o);
}
}
monitor
在我得到的 getList 调用期间调试变量
monitor => instance of correct class
fields:
CGLIB$BOUND => true
CGLIB$CALLBACK_0.advised => proxyFactory (correct)
CGLIB$CALLBACK_1.target (reference to the correct instance of myStatefulService class)
fields:
list => [.........] (correctly populated)
CGLIB$CALLBACK_2 .....
......
......
......
list => [] (the list that would be populated is empty instead)