Java新建Maven项目学习Spring注解开发显示:Caused by: java.lang.reflect.InaccessibleObjectException

发生缘由

  • 学习Spring中IOC/DI注解开发

环境

  • jdk版本:jdk-16.0.2
  • Idea版本:2021.2
  • 电脑系统:win10

问题及补救

问题描述

新建如下类和接口:

public interface BookDao {
   public void save();
}
@Component("bookDao")
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("book dao save ...");
    }
}
@Configuration
@ComponentScan("com.linxuan.dao.impl")
public class SpringConfig {
}
public class AppForAnnotation {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        System.out.println(bookDao);
    }
}

运行主方法却发现报错,报错信息如下:

Exception in thread "main" java.lang.IllegalStateException: Cannot load configuration class: com.linxuan.config.SpringConfig
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:410)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:263)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:130)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
	at com.linxuan.domain.App.main(App.java:12)
Caused by: java.lang.ExceptionInInitializerError
	at org.springframework.cglib.core.KeyFactory$Generator.getProtectionDomain(KeyFactory.java:136)
	at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:206)
	at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:149)
	at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:117)
	at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:109)
	at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:105)
	at org.springframework.cglib.proxy.Enhancer.<clinit>(Enhancer.java:71)
	at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer(ConfigurationClassEnhancer.java:119)
	at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:107)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:400)
	... 7 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @335eadca
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
	at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
	at org.springframework.cglib.core.ReflectUtils$1.run(ReflectUtils.java:52)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
	at org.springframework.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:42)
	... 17 more

报错信息

解决方案

  • 笔者使用的JDK版本是16,而反射自从JDK8之后就有了限制:JDK 9将类型和资源封装在模块中。
  • JDK9以上模块不能使用反射去访问非公有的成员/成员方法以及构造方法,除非模块标识为opens去允许反射访问。否则会产生非法反射警告。

解决方法如下:

  1. 点击右上角的Select Run/Debug Configuration
  2. 点击Edit Configurations...
  3. 点击Modify options
  4. 点击Add VM options
  5. 这时会在Run/Debug Configurations窗口上面多出现一个名为Add VM options的方框。我们只需要在里面键入下面代码即可: --add-opens java.base/java.lang=ALL-UNNAMED 
  6. 点击Apply,点击OK。右键运行,那么就没有问题了。

————————————————
版权声明:本文为CSDN博主「此方星河」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_51426055/article/details/124419048

posted @ 2022-04-26 09:13  炸天帮帮主  阅读(1829)  评论(1编辑  收藏  举报