Java17下Spring Boot2.7.3整合JavaFx遇CGLIB访问权限问题求助
Java 17整合Spring Boot 2.7.3与JavaFX时模块访问错误的解决方法
错误详情
class com.XXX$$EnhancerBySpringCGLIB$$637d19c4 (in module com.xxx) cannot access class org.springframework.cglib.core.ReflectUtils (in unnamed module @0x2d950574) because module com.xxx does not read unnamed module @0x2d950574
解决方法
方法1:修改模块声明文件(module-info.java)
在你的模块com.xxx的module-info.java中添加配置,允许模块读取Spring CGLIB相关类,并开放代理类所在包给CGLIB:
module com.xxx { // 保留原有requires声明,新增以下内容 requires org.springframework.cglib; opens com.xxx to org.springframework.cglib; }
如果Spring CGLIB处于未命名模块,可直接添加对所有未命名模块的读取权限:
module com.xxx { // 其他配置 requires transitive ALL-UNNAMED; }
方法2:添加JVM启动参数
在应用启动时添加JVM参数,让你的模块允许读取未命名模块:
--add-reads com.xxx=ALL-UNNAMED
如果使用构建工具,可在配置中指定该参数:
- Maven(pom.xml):
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments>--add-reads com.xxx=ALL-UNNAMED</jvmArguments> </configuration> </plugin>
- Gradle(build.gradle):
bootRun { jvmArgs = ['--add-reads com.xxx=ALL-UNNAMED'] }
方法3:禁用CGLIB代理,改用JDK动态代理
在Spring Boot配置文件中添加配置,强制Spring使用JDK动态代理:
- application.properties:
spring.aop.proxy-target-class=false
- application.yml:
spring: aop: proxy-target-class: false
注意:此方法要求被代理类必须实现接口,否则Spring仍会尝试使用CGLIB。
内容的提问来源于stack exchange,提问作者Aguid
相关产品推荐
相关产品推荐

