是否可以禁止注释继承?

时间:2011-06-09 10:44:07

标签: java inheritance annotations

我有一个控制器超类及其子类:

public class SuperController {
    @Resource
    private A resourceA;
}

public class SubController extends SuperController {
    @Resource
    private B resourceB;
}

我不在resourceA中使用Subcontroller字段,但Subcontroller充当SuperController且具有相同的方法。那么我怎么能绕过@Resource注释的继承,就像那样:

public class SuperController {
    **private** @Resource
    private A resourceA;
}

1 个答案:

答案 0 :(得分:1)

如果子类不使用父类的某些字段,那么你需要一个通用的抽象类。

public abstract class AbstractController {
    // common fields.
    // common methods.
}

public class SuperController extends AbstractController {
    @Resource
    private A resourceA;
}

public class SubController extends AbstractController {
    @Resource
    private B resourceB;
}