我需要一个服务(单身符合)与一些内部字段,如待处理线程列表(是的一切都写成线程安全)问题是,如果我@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);
}
}
中的GetList通话过程中调试变量monitor
我得到
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)
对不起,列表中的@autowired是一个打字错误....一切都在Spring的控制之下,“某人”是一种响应客户端动作的其他服务......所以客户端使用其他服务向列表添加元素。带有列表的服务包含逻辑并定期在列表上工作,所以我需要成为一个singleton可注入有状态bean =) – Gnappuraz 2012-07-20 14:22:18
是的,我提供了添加元素到列表的方法,但是当列表被读取时它也是空的,如果代理指向的实例已正确填充它。 – Gnappuraz 2012-07-20 14:29:01
为什么你需要一个代理?我会说这是一个不需要由Spring控制的对象的例子。只是实例化一个新的并继续下去。 – duffymo 2012-07-20 16:50:26