相关疑难解决方法(0)

spring单例bean字段未填充

我需要一个服务(单身适合)和一些内部字段,比如挂起的线程列表(是的,所有内容都写成线程安全的)问题是如果我@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);
   }
}
Run Code Online (Sandbox Code Playgroud)

monitor我得到的getList调用期间调试变量

monitor => instance of correct class
 fields:
   CGLIB$BOUND => true
   CGLIB$CALLBACK_0.advised => proxyFactory …
Run Code Online (Sandbox Code Playgroud)

java spring

2
推荐指数
1
解决办法
3687
查看次数

如何打破超类构造链?

class Animal {
    protected Animal(){
        System.out.println("ANIMAL CONSTRUCTOR");
    }
    public void move(){
        System.out.println("ANIMAL Move");
    }
}

class Dog extends Animal{
    public Dog(){
        System.out.println("Dog Constructor");
    }
    public void move(){
        System.out.println("Dog move");
    }

}


public class Test {
    public static void main(String args[]){
    Dog d = new Dog();
    d.move();
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码产生以下结果:

ANIMAL CONSTRUCTOR 
Dog Constructor
Dog move
Run Code Online (Sandbox Code Playgroud)

似乎在创建dog实例时,它默认情况下也会调用Animal构造函数(隐式).

这很奇怪,因为我在考虑明确地打电话super()可以做同样的工作.

有没有办法打破这个构造函数链,让我的狗实例只调用Dog构造函数?

如果没有,是否有理由呢?

java inheritance class

2
推荐指数
1
解决办法
315
查看次数

标签 统计

java ×2

class ×1

inheritance ×1

spring ×1