我有一个控制器超类及其子类:
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;
}
答案 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;
}