提问人:Kaustubh Choudhary 提问时间:8/14/2023 最后编辑:Kaustubh Choudhary 更新时间:8/14/2023 访问量:147
java.lang.reflect.InaccessibleObjectException 时使用 @Configuration
java.lang.reflect.InaccessibleObjectException while using @Configuration
问:
我通过以下示例向我的学生解释基于 Java 的配置。
我正在使用 JDK 17.0.7,Spring 版本 5.3.29。
有趣的是:如果我不@Configuration而是使用@Component或@Configurable它工作正常
文件 1:第 1 类
package java_config;
public class Book {
private String name;
private double price;
public Book() {
super();
}
public Book(String name, double price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + "]";
}
}
文件 2:第 2 类
package java_config;
public class Student {
private String name;
private String subject;
private Book book;
public Student() {
super();
}
public Student(String name, String subject, Book book) {
super();
this.name = name;
this.subject = subject;
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "Student [name=" + name + ", subject=" + subject + ", book=" + book + "]";
}
}
文件 3:第 3 类
package java_config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
public class JavaConfig {
@Bean
public Book getBook() {
return new Book("Let Us C", 200);
}
@Bean
public Student getStudent() {
return new Student("Ashutosh", "PCM", getBook());
}
}
文件 4 : 第 4 类
package java_config;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class JavaConfigurationDemo {
public static void main(String[] args) {
**// The below line is showing the exception
ApplicationContext ctx =
new AnnotationConfigApplicationContext(JavaConfig.class);**
Student student = ctx.getBean("getStudent", Student.class);
System.out.println("Student's book name : " + student.getBook().getName());
}
}
输出:控制台中的异常
Exception in thread "main" org.springframework.cglib.core.CodeGenerationException: 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 @73f792cf
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:600)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108)
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:572)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:419)
at org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:137)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:109)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:447)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:268)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:325)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:147)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:748)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:93)
**at java_config.JavaConfigurationDemo.main(JavaConfigurationDemo.java:10)**
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 @73f792cf
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
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.defineClass(ReflectUtils.java:552)
... 22 more
答:
0赞
Mohamed amine ben hassen
8/14/2023
#1
由于上面的 Java 9 抽象包发生了变化,所以基本上你必须配置跨模块访问类和方法的权限。
https://developer.ibm.com/tutorials/java-modularity-2/
评论
0赞
Kaustubh Choudhary
8/15/2023
这就像一个谜。我创建了一个新项目并将这些文件复制到一个新包中,但仍然出现了同样的错误。然后我创建了一个新包并创建了所有类。然后我以以下方式复制了新文件中的所有旧内容 - 全选 + 全部复制 + 全部粘贴。令人惊讶的是,在新包中,代码就像一个魅力。旧的仍然给出例外。两个包中没有代码差异。我不知道出了什么错误。
0赞
Kaustubh Choudhary
8/15/2023
在这里找到你想试一试的代码:drive-link
评论