我在 Spring Controller 中使用内部类。从它的父类超类访问受保护的字段/方法时遇到问题。
研究表明,这是由某种方式的不同类加载器引起的,但我对 Spring 的了解还不够确定。
class SuperBaseController {
protected String aField;
protected void aMethod() {
}
}
@Controller
class OuterMyController extends SuperBaseController {
class Inner {
public void itsMethod() {
// java.lang.IllegalAccessError: tried to access method
aMethod();
}
public void getField() {
// java.lang.IllegalAccessError: tried to access field
String s = aField;
}
}
void doSomething () {
// Obviously fine.
aMethod();
// Fails in the Inner method call.
new Inner().itsMethod();
// Obviously fine.
String s = aField;
// Fails in the Inner method call.
new Inner().getField();
}
}
是否有任何简单的技术可以避免/解决此问题?最好是那些不涉及制作字段/方法的public
。
我已经确认ClassLoader
外部类的属性与超类的属性不一样。