I am using an inner class in a Spring Controller. It is having problems accessing protected fields/methods from it's parent classes super class.
Research suggests that this is caused by differing class-loaders in some way but I don't know enough about Spring to be certain.
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();
}
}
Are there any simple techniques to avoid/fix this issue? Preferably ones that do not involve making the fields/methods public.
I have confirmed that the ClassLoader attributes of the outer class is not the same as that of the super class.
I created the following classes:
I tried to invoke the rest service and I had no issue. Now I'm using this configuration for Spring (annotation based annotation):
As you can see I used the
@Importannotation and in my web.xml I use this configuration:By using this configuration I had no issue in invoking protected fields and/or method inside an inner class
If you can't adapt your configuration to this one.. may you post the configuration you used?
I hope it can be useful
Angelo