这两天在尝试做微服务的项目时,遇到些小问题
使用环境为JDK11,启动微服务的时候服务的控制台中出现下列警告:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/D:/Software/Maven/apache-maven-3.6.1/mvn_repo/com/thoughtworks/xstream/xstream/1.4.10/xstream-1.4.10.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

解决方案:
在对应微服务的启动类下添加禁用警告方法:
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
}
}
在从这个启动类中调用这个方法:
public static void main(String[] args) {
//禁用警告
disableWarning();
SpringApplication.run(EmpServiceApplication.class, args);
}
微服务启动成功
在使用JDK11开发微服务时遇到非法反射访问警告,通过在启动类中添加禁用警告方法并调用,成功解决了问题,确保了SpringApplication的正常运行。
4503

被折叠的 条评论
为什么被折叠?



