我正在将 Tomcat Web 应用程序从 Tomcat 9.0.78 升级到 10.0.14,并将所有导入从 javax.servelet 命名空间移至 jakarta.servelt 命名空间,并且还升级了 hibernate。
虽然我已经能够解决大多数问题,但我遇到了在 /tomcat/logs/localhost.2023-09-12.log 中看到的以下错误
我尝试从 tomcat/lib 中删除 jar,例如 jakarta.inject-2.6.1.jar 和 jakarta.inject-api-2.0.0.jar,但这些会导致类未找到异常。
我认为该错误意味着两个不同的地方正在尝试实例化 org.glassfish.jersey.inject.hk2.RequestContext 但我不知道在哪里。
编辑:
查看源代码后,org.glassfish.jersey.inject.hk2.RequestContext 被创建两次并不是问题,而是它没有实现 jakarta.singleton 空间。
根据 jan.supol 的评论,所有球衣罐都是 3.0.2 版本,其中一个包含有问题的类。
jersey-hk2-3.0.2.jar:
877 01-01-1980 00:00 org/glassfish/jersey/inject/hk2/RequestContext$Binder$1.class
1170 01-01-1980 00:00 org/glassfish/jersey/inject/hk2/RequestContext$Binder.class
4459 01-01-1980 00:00 org/glassfish/jersey/inject/hk2/RequestContext.class
-rwxrwxr-x 1 root tomcat 31856 Sep 6 11:11 jersey-container-servlet-3.0.2.jar
-rwxrwxr-x 1 root tomcat 1170111 Sep 6 11:11 jersey-common-3.0.2.jar
-rwxrwxr-x 1 root tomcat 75633 Sep 6 11:11 jersey-hk2-3.0.2.jar
-rwxrwxr-x 1 root tomcat 72355 Sep 6 11:11 jersey-container-servlet-core-3.0.2.jar
-rwxrwxr-x 1 root tomcat 934996 Sep 6 11:11 jersey-server-3.0.2.jar
-rw-rw-r-- 1 vcr vcr 94880 Sep 14 12:17 jersey-media-jaxb-3.0.2.jar
-rw-rw-r-- 1 vcr vcr 79408 Sep 14 12:17 jersey-media-sse-3.0.2.jar
-rw-rw-r-- 1 vcr vcr 26118 Sep 14 12:17 jersey-media-json-binding-3.0.2.jar
023 12:14:49.238 SEVERE [main] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [jersey-serlvet] in web application [/ngen] threw load() exception
java.lang.IllegalArgumentException: The implementation class org.glassfish.jersey.inject.hk2.RequestContext must be in the Singleton scope
at org.jvnet.hk2.internal.ServiceLocatorImpl.checkConfiguration(ServiceLocatorImpl.java:1700)
at org.jvnet.hk2.internal.ServiceLocatorImpl.addConfiguration(ServiceLocatorImpl.java:2095)
at org.jvnet.hk2.internal.DynamicConfigurationImpl.commit(DynamicConfigurationImpl.java:262)
at org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:166)
at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.<init>(AbstractHk2InjectionManager.java:65)
at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.<init>(ImmediateHk2InjectionManager.java:38)
at org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory$Hk2InjectionManagerStrategy$1.createInjectionManager(Hk2InjectionManagerFactory.java:55)
at org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory.create(Hk2InjectionManagerFactory.java:73)
at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:69)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:347)
at jakarta.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1165)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1118)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1011)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4906)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5213)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1188)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2023)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1098)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:480)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1708)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:320)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:946)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:886)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:795)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
以下是我对玻璃鱼的依赖:
<h2k.version>3.0.2</h2k.version>
<h2k.locator.version>3.0.1</h2k.locator.version>
<jakarta.inject.version>2.6.1</jakarta.inject.version>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
<version>${h2k.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>${h2k.locator.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-utils</artifactId>
<version>${h2k.version}</version>
</dependency>
我通过根据我的 Tomcat 版本定位所有 jar 来修复此问题。我发现我的 tomcat 版本使用 Jakarta ee 9,并且我的所有第三方 jar 都围绕它。
具体来说,就我而言,罪魁祸首是 hk2 jar 的版本。