java - 内部类访问 protected 外部类super时如何避免 `IllegalAccessError`

标签 java spring inner-classes superclass illegalaccessexception

我在 Spring Controller 中使用内部类。从其父类父类(super class)访问 protected 字段/方法时遇到问题。

研究表明这是由不同的类加载器以某种方式引起的,但我对 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 属性与父类(super class)的属性不相同。

最佳答案

我创建了以下类:

public class BaseController
{
    protected String field;
    protected void method()
    {
        System.out.println("I'm protected method");
    }
}

@RestController
@RequestMapping("/stack")
public class StackController extends BaseController
{
    class Inner
    {
        public void methodInvocation()
        {
            method();
        }
        public void fieldInvocation()
        {
            field = "Test";
        }
    }
    @RequestMapping(value= {"/invoca"}, method= {RequestMethod.GET})
    public ResponseEntity<String> invocation()
    {
        Inner in = new Inner();
        in.fieldInvocation();
        in.methodInvocation();
        return new ResponseEntity<String>("OK", HttpStatus.OK);
    }
}

我尝试调用其余服务,没有遇到任何问题。现在我正在为 Spring 使用此配置(基于注释的注释):

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "it.spring.controller" })
@PropertySource( value={"classpath:config.properties"}, encoding="UTF-8", ignoreResourceNotFound=false)
public class DbConfig
{
}


@Configuration
@EnableWebMvc
@Import(DbConfig.class)
@PropertySource(value = { "classpath:config.properties" }, encoding = "UTF-8", ignoreResourceNotFound = false)
public class WebMvcConfig extends WebMvcConfigurerAdapter
{
}

如您所见,我使用了 @Import 注释,并在我的 web.xml 中使用了以下配置:

<servlet>
    <servlet-name>SpringDispSvlt</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>it.WebMvcConfig</param-value>
    </init-param>
    <init-param>
        <param-name>dispatchOptionsRequest</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

通过使用此配置,我可以毫无问题地调用内部类中的 protected 字段和/或方法

如果你不能使你的配置适应这个..你可以发布你使用的配置吗?

希望对你有用

安杰洛

关于java - 内部类访问 protected 外部类super时如何避免 `IllegalAccessError`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713360/

相关文章:

java - 查找数组中不存在空值的索引

java - Spring Boot : statistics of APIs using http. 服务器请求

java - 如何在 Spring Security 中使用电子邮件作为用户名?

Spring MVC : how to get case-insensitive ordering from Pageable

java - 提供的 .class 文件中出现 NoClassDefFoundException

java - 启动外部 .exe 并实时读取控制台中写入的内容

java - 我如何使用 Retrofit 2.3.0 解析 json 对象

Java Android - Redmi 3 (MIUI) - 无法更改通知图标?

python - python中的内部类评估顺序

Java 内部类