Spring Boot 警告:An illegal reflective access operation has occurred
阿新 • • 發佈:2020-11-24
Spring Boot 警告:An illegal reflective access operation has occurred
Spring Boot專案升級到JDK 11,執行時發現警告如下:
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/pc-bjdev/.m2/repository/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar) to field java.util.TreeMap.comparator WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
解決方案
- 方案一
增加 JVM 啟動引數:
java --illegal-access=deny
Java 9 中這個引數預設是:permit
- 方案二
在啟動類增加以下方法,親測試 jdk 11 有效
public static void disableWarning() { try { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); Unsafe u = (Unsafe) theUnsafe.get(null); Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger"); Field logger = cls.getDeclaredField("logger"); u.putObjectVolatile(cls, u.staticFieldOffset(logger), null); } catch (Exception e) { // ignore } }
啟動 Spring Boot 時呼叫一下上面的方法
public static void main(String[] args) {
disableWarning(); //禁用警告
SpringApplication.run(AppApplication.class, args);
}