本文介绍了从方法引用的 java.lang.IllegalArgumentException 在类加载器中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在/target/class/..... 中通过 wsimport 为 WS 服务生成存根并使用 devtools 运行 Spring Boot 应用程序时遇到异常.

I obtained an exception when generated a stub for a WS service by wsimport in /target/class/..... and run a spring boot application with devtools.

Caused by: java.lang.IllegalArgumentException: com....从方法引用的服务在类加载器中不可见

我发现 spring devtools 类加载器存在问题,RestartClassLoader,因为对类的两个不同引用(RestartClassLoader 和 AppClassLoader)

I found that an issue with spring devtools class loader, RestartClassLoader, because of two different references to a Class (RestartClassLoader and AppClassLoader)

private static void ensureVisible(ClassLoader ld, Class<?> c) {
    Class<?> type = null;
    try {
        type = Class.forName(c.getName(), false, ld);
    } catch (ClassNotFoundException e) {
        if (type != c) {
            throw new IllegalArgumentException(c.getName() +
                    " referenced from a method is not visible from class loader");
        }
    }
}

我试图在 spring-devtools.properties 中添加对 jar 文件的引用以重新启动.include=/.....jar

I was trying to add a reference to a jar file in spring-devtools.properties to restart.include=/.....jar

Spring Boot 2.0.0.RELEASE Java 9

推荐答案

由于它们是生成的类,因此您必须将它们从 Spring Devtools重新启动"类加载器中排除.

Since they are generated classes you have to exclude them from Spring Devtools "restart" classloader.

  1. 创建一个/src/main/resources/META-INF/spring-devtools.properties文件
  2. 添加诸如 restart.exclude.* 之类的属性以从重启类加载器中排除类(例如,您可以使用 restart.exclude.mygeneratedclasses=/*[generated]*.class 排除所有带有 generated 词作为包或类名的一部分的类)

  1. Create a /src/main/resources/META-INF/spring-devtools.properties file
  2. Add properties like restart.exclude.* to exclude classes from restart classloader (example, you can use restart.exclude.mygeneratedclasses=/*[generated]*.class to exclude all classes with generated word as part of package or class name)

完成.现在,您可以使用 devtools,并且 WS 生成的类没有问题.

Done. Now you can use devtools and have no issues with WS generated classes.

参考:

[1] https://github.com/spring-projects/spring-boot/issues/4529

[2] https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-customizing-classload

这篇关于从方法引用的 java.lang.IllegalArgumentException 在类加载器中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:47