在抽象超类中注入依赖失败

时间:2014-08-28 13:37:22

标签: java spring dependency-injection

我有像

这样的类的层次结构
public interface IX
{
  public String getSomething();
}
public class X implements IX
{

  @Override
  public String getSomething()
  {
    return "something";
  }

}

public abstract class A implements IX
{
  private X x;

  public X getX()
  {
    return x;
  }

  public void setX(X x)
  {
    this.x = x;
  }
  @Override
  public String getSomething()
  {
    return x.getSomething(); **//NullPointerException here ****
  }
}

public class BaseB extends A
{
  private Y y;

  public Y getY()
  {
    return y;
  }

  public void setY(Y y)
  {
    this.y = y;
  }
}

public class B extends BaseB
{
  public void test()
  {
    String something = getSomething();
  }
}

我在XML中跟随

<bean id="x" class="X" />
<bean id="y" class="Y" />
<bean class="A" id="a" abstract="true" >
    <property name="x" ref="x" />
</bean>

<bean class="B" id="b" parent="a" >
    <property name="y" ref="y" />
</bean>

依赖注入有问题,X没有注入,并且正如我在上面的代码中提到的那样抛出NPE。 我做错了什么?

0 个答案:

没有答案