gpt4 book ai didi

java - findResource ("") 当 module-info.java 存在时返回 null,这是为什么呢?

转载 作者:IT老高 更新时间:2023-10-28 20:31:36 30 4
gpt4 key购买 nike

我正在调试为什么在我的 Spring Boot 应用程序中存在 module-info.java 时,spring-orm 在启动期间会引发异常。这是一个异常(exception):

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.context@5.0.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.8.RELEASE.jar:na]
at spring.context@5.0.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.8.RELEASE.jar:na]
at spring.context@5.0.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.8.RELEASE.jar:na]
at spring.boot@2.0.4.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:na]
at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:na]
at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:na]
at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:na]
at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:na]
at spring.boot@2.0.4.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:na]
at tech.flexpoint.dashmanserver/tech.flexpoint.dashmanserver.DashmanServerApplication.main(DashmanServerApplication.java:13) [classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at spring.boot.devtools@2.0.4.RELEASE/org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.4.RELEASE.jar:na]
Caused by: java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3119) ~[na:na]
at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3144) ~[na:na]
at java.base/java.lang.Class.getMethods(Class.java:1863) ~[na:na]
at hibernate.core@5.2.17.Final/org.hibernate.service.internal.AbstractServiceRegistryImpl.applyInjections(AbstractServiceRegistryImpl.java:288) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:279) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:239) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:80) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.internal.SessionFactoryImpl.canAccessTransactionManager(SessionFactoryImpl.java:942) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.internal.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:953) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:319) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462) ~[hibernate-core-5.2.17.Final.jar:na]
at hibernate.core@5.2.17.Final/org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892) ~[hibernate-core-5.2.17.Final.jar:na]
at spring.orm@5.0.8.RELEASE/org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.8.RELEASE.jar:na]
at spring.orm@5.0.8.RELEASE/org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.8.RELEASE.jar:na]
at spring.orm@5.0.8.RELEASE/org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.0.8.RELEASE.jar:na]
at spring.orm@5.0.8.RELEASE/org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.0.8.RELEASE.jar:na]
at spring.orm@5.0.8.RELEASE/org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.8.RELEASE.jar:na]
at spring.beans@5.0.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.8.RELEASE.jar:na]
... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.transaction.UserTransaction
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499) ~[na:na]
... 42 common frames omitted

如果 module-info.java 存在,我将问题追踪到 URLClassLoader.findResource("") 返回 null"file:/C:/Users/pupeno/Documents/Dashman/code/dashmanserver/target/classes/" 如果不是。

我创建了引发相同异常的最小可能示例。要运行它,您需要:

  1. 从此处克隆并安装 Moditect 的最新副本:https://github.com/moditect/moditect由于此错误修复尚未发布:https://github.com/moditect/moditect/issues/51
  2. https://github.com/dashmantech/demo 克隆演示代码库
  3. 使用凭据 demo/confi/application.properties 设置本地 PostgreSQL 数据库
  4. 先运行mvn clean package,ModiTec创建所有模块
  5. 在最新的 IntelliJ 副本中打开项目
  6. 单击“运行演示”配置文件的播放(.idea 目录包含在相应的运行配置文件中,带有参数等)。

我需要 findResource("") 来返回 "file:/C:/Users/pupeno/Documents/Dashman/code/dashmanserver/target/classes/"以便 spring-orm 可以工作。

findResource("") 看起来像这样:

public URL findResource(final String name) {
/*
* The same restriction to finding classes applies to resources
*/
URL url = AccessController.doPrivileged(
new PrivilegedAction<>() {
public URL run() {
return ucp.findResource(name, true);
}
}, acc);

return url != null ? URLClassPath.checkURL(url) : null;
}

所以我可以看到在不使用模块系统的情况下可以进行一些访问,但是当存在 module-infe.java 时,Java 的模块系统会阻止它。我的问题是我不知道如何使它工作,应该导出或打开什么才能工作?

Spring Boot 导致该方法调用的方式是通过 RestartClassLoaderURLClassLoader 的子类,具体来说,第 124 行调用 super.findResource(名称) 在:

@Override
public URL findResource(String name) {
final ClassLoaderFile file = this.updatedFiles.getFile(name);
if (file == null) {
return super.findResource(name);
}
if (file.getKind() == Kind.DELETED) {
return null;
}
return AccessController
.doPrivileged((PrivilegedAction<URL>) () -> createFileUrl(name, file));
}

正在使用的特定 RestartClassLoader 实例是 ClassPathResource 的成员,它是这样定义的:

this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());

在构造函数中,line 85 .

最后,getDefaultClassLoader() 看起来像这样:

/**
* Return the default ClassLoader to use: typically the thread context
* ClassLoader, if available; the ClassLoader that loaded the ClassUtils
* class will be used as fallback.
* <p>Call this method if you intend to use the thread context ClassLoader
* in a scenario where you clearly prefer a non-null ClassLoader reference:
* for example, for class path resource loading (but not necessarily for
* {@code Class.forName}, which accepts a {@code null} ClassLoader
* reference as well).
* @return the default ClassLoader (only {@code null} if even the system
* ClassLoader isn't accessible)
* @see Thread#getContextClassLoader()
* @see ClassLoader#getSystemClassLoader()
*/
@Nullable
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch (Throwable ex) {
// Cannot access thread context ClassLoader - falling back...
}
if (cl == null) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
if (cl == null) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
}
return cl;
}

我的 module-info.java 包含:

module tech.flexpoint.dashman {
exports tech.flexpoint.dashman to com.fasterxml.jackson.databind;
exports tech.flexpoint.dashman.controllers.configurator to javafx.fxml;

opens tech.flexpoint.dashman to javafx.graphics, jna;
opens tech.flexpoint.dashman.controllers.common to javafx.fxml;
opens tech.flexpoint.dashman.controllers.configurator to javafx.fxml;
opens tech.flexpoint.dashman.models to org.hibernate.validator, tech.flexpoint.dashmancommon, javafx.base;

opens common;
opens configurator;
opens displayer;
opens winscreensaver;

requires appdirs;
requires org.bouncycastle.provider;
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.datatype.jdk8;
requires io.sentry;
requires jackson.annotations;
requires java.desktop;
requires java.sql;
requires java.validation;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires javafx.media;
requires javafx.web;
requires jna;
requires jna.platform;
requires org.apache.commons.lang3;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.ikonli.fontawesome5;
requires spring.core;
requires spring.retry;
requires spring.web;
requires tech.flexpoint.dashmancommon;
}

在 IntelliJ 中我启用了这些插件:

  • Lombok 插件
  • .ginore
  • PowerShell
  • VisualVM 启动器
  • ANSI 荧光笔
  • 批处理脚本支持
  • 字节码查看器
  • CMD 支持
  • 版权所有
  • 覆盖范围
  • CSS 支持
  • 数据库工具和 SQL
  • Git 集成
  • GitHub
  • 分级
  • 时髦的
  • Heroku 集成
  • HTML 工具
  • HTTP 客户端
  • 用于 Java 的 l18n
  • IDE 设置同步
  • Java 字节码反编译器
  • Java EE:EJB、JPA、Servlet
  • Java 流调试器
  • JavaFX
  • JUnit
  • 行排序器
  • Markdown 支持
  • Maven 集成
  • Maven 集成扩展
  • 持久性框架支持
  • 属性支持
  • Smali 支持
  • Spring AOP/@AspectJ
  • Spring 批处理
  • Spring 启动
  • Spring 数据
  • Spring 集成模式
  • Spring OSGi
  • Spring 安全性
  • Spring 支持
  • Spring Web 服务
  • Spring WebSocket
  • 终端
  • YAML

最佳答案

我注意到的一件事是您的应用程序(假设它被打包在 tech.flexpoint.dashman 中)似乎没有以任何方式向 Spring 开放,这肯定会导致类失败加载/非法访问。

我希望在 module-info.java 中看到类似的内容(取决于您的 Spring 依赖项):

opens tech.flexpoint.dashman to spring.core, spring.beans, spring.context;

异常是一个NoClassDefFoundError,当一个在编译时已知的类的类定义无法解析时,在运行时抛出该异常,在这种情况下是接口(interface)javax.transaction.UserTransaction,它是 Java Transaction API (JTA) 的一部分。

正如其他人所指出的,JTA 没有与 JDK 捆绑在一起,需要作为编译依赖项添加。但是,需要加载 UserTransaction 类定义的类来自 spring-boot-autoconfigure 工件,该工件负责自己的依赖项( spring-boot-autoconfigure@2.4.0.RELEASE 🡒 jboss-transaction-spi@7.6.0.Final 🡒 jboss-transaction-api_1.2_spec@1.1.1.Final ) ,所以你应该不需要需要添加JTA作为依赖。

但是,因为您想将自己的应用程序打包为 Java 9 模块,所以它需要 explicitly state its dependenciesspring-boot-autoconfigure 还不是一个模块化的 Java 9 库,并且不会为您执行此操作(即传递性)。 JTA的自动模块名是java.transaction,所以需要在module-info.java中添加需求:

requires java.transaction;

我让您的示例运行,并且在从 IntelliJ IDEA 运行时确实得到了 NoClassDefFoundError。堆栈跟踪指向一个 ClassNotFoundException,它指示类路径问题。由于 IDEA 在从那里启动应用程序时会计算类路径,所以我想看看在使用 spring-boot-maven-plugin 运行应用程序时是否可以重现错误。

我将IDEA运行配置复制到spring-boot-maven-plugin配置中,如下图:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>tech.flexpoint.demo.DemoApplication</mainClass>
<jvmArguments>--show-module-resolution --add-opens=java.base/java.lang=spring.core --add-opens=java.base/java.io=tomcat.embed.core --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED</jvmArguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</plugin>

然后我调用 mvn spring-boot:run 瞧,应用程序成功启动,没有错误。我只能得出结论,这是 IntelliJ 计算的类路径的问题。

关于java - findResource ("") 当 module-info.java 存在时返回 null,这是为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51944963/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com