org.springframework.cglib.core.ReflectUtils$1 的非法反射访问

Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1

我的 JDK 9+181 Spring Boot 2.0.0.BUILD-SNAPSHOT CLI 应用程序在启动时显示此警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils (jar:file:/home/jan/src/fm-cli/target/fm-cli-0.1.0-SNAPSHOT.jar!/BOOT-INF/lib/spring-core-5.0.0.RELEASE.jar!/) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils

这是一个控制台应用程序,所以我需要禁用此警告 -- 我该怎么做?

注:本题问的是如何禁用Spring触发的这个warning的具体问题;它不是 JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState 的副本,它处理不同库中的类似症状。

在 JDK 9+ 中,将以下选项添加到 JVM 以禁用 Spring 使用 CGLIB 的警告:

--add-opens java.base/java.lang=ALL-UNNAMED

例如:

java --add-opens java.base/java.lang=ALL-UNNAMED -jar target/*.jar

无需举报;这是 known Spring bug

发生这种情况是因为新的 JDK 9 模块系统检测到非法访问,该访问将在(不久的)将来的某个时候被禁止。您可以阅读有关 the JDK 9 Module system here.

的更多信息

更新:

此问题已修复 JDK 9+ 和 Spring 5.1+。

添加到上面的 Jan Nielsen 回答中,如果您使用的是 Intellij 和 Spring Boot 2.0.3,它依赖于 Spring core 5.0.7,您仍然卡住并且没有使固定。

我的出路需要两件事:

  • 将 Jan 提到的 --add-opens 添加到您的 run/debug 配置中。只需编辑配置并查看环境/VM 选项。这会消除一些 "illegal access messages".

  • 我还需要向 jaxb-api 库添加依赖项。我从 ValentinBossi 对 this spring github issue.

  • 的评论中得到提示

使用 spring 初始化程序时,确保使用最新版本的 Spring Boot.它会自动为您获取 Spring 核心 5.1 或更高版本,并且当您 运行 项目时您不会看到错误。

因此您不必担心编辑任何 JVM 配置。

在这些警告之后,如果您的应用仍然无法运行,请将此依赖项添加到您的 pom.xml

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

这对我有帮助!!

我在 JDK 11 和 Spring Boot 2.2.1(Spring Core 5.2.1)上也发生过这种情况。

有用的是删除对 org.springframework.boot:spring-boot-devtools 的依赖,如 some comments to Spring Framework issue #22814 中所建议的那样。

我正在使用这些版本,但仍然看到警告:

  • Spring启动 2.4.1
  • Spring Core FW: 5.3.2 (我的parent pom是Spring Boot的,所以这个版本是它引入的,不是我手动设置的)
  • Java openjdk 版本“11.0.9.1” 2020-11-04

从 spring-boot-devtools 中删除依赖项对我也有效,启动时警告不再存在。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

... 在一天结束时,我只是在类路径发生任何更改时使用 IDE 的自动重新启动,我可以不用它就离开。 不过,理论上我们不应该为此担心,因为根据 Spring documentation:

Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it’s started using a special classloader, then it is considered a “production application”

因此可以预期,当从命令行启动应用程序时,警告应该消失(我尚未验证)。

如果你使用maven和spring,请升级到Spring Framework 5.1+ / Spring Boot 2.1+.