我目前正在将项目从 Java 11 迁移到 Java 17、从 Spring 5 迁移到 Spring 6、从 RichFaces 迁移到 PrimeFaces 14(雅加达)。进行必要的更改后,我遇到以下异常:
Exception in thread "main" java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityBean': Invocation of init method failed
at com.leo.service-ui.Main.main(Main.java:27)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityBean': Invocation of init method failed
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1798)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:971)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
at com.leo.service-ui.Main.main(Main.java:24)
Caused by: java.lang.NoClassDefFoundError: jakarta/enterprise/inject/spi/el/ELAwareBeanManager
at jakarta.faces.CurrentThreadToServletContext.getFactoryFinder(CurrentThreadToServletContext.java:52)
at jakarta.faces.FactoryFinder.getFactory(FactoryFinder.java:257)
at jakarta.faces.context.FacesContext.getCurrentInstance(FacesContext.java:872)
at com.leo.service-ui.util.FacesUtil.getRequestParameter(FacesUtil.java:28)
at com.leo.service-ui.ui.beans.EntityBean.init(EntityBean.java:76)
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:569)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMethod.invoke(InitDestroyAnnotationBeanPostProcessor.java:457)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:401)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:219)
... 16 more
Caused by: java.lang.ClassNotFoundException: jakarta.enterprise.inject.spi.el.ELAwareBeanManager
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
... 28 more
pom.xml
:
<dependencies>
<!-- Other existing dependencies -->
<!-- Jakarta EE dependencies -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>14.0.6</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>14.0.6</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
<version>4.1.0-RC2</version>
</dependency>-->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.faces</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>jakarta.el</groupId>
<artifactId>jakarta.el-api</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
以下是发生异常的相关代码:
public class FacesUtil {
public static String getRequestParameter(String paramName) {
FacesContext context = FacesContext.getCurrentInstance(); // Exception occurs here
Map<String, String> params = context.getExternalContext().getRequestParameterMap();
return params.getOrDefault(paramName, null);
}
}
pom.xml
。 2. 确保存在
weld-servlet-shaded
依赖项。
3. 添加了 jakarta.jakartaee-api
依赖关系,范围为 provided
。
这可能是什么原因造成的
java.lang.ClassNotFoundException
我该如何解决?迁移到 Java 17、Spring 6 和 PrimeFaces 14(雅加达)时是否需要任何额外的依赖项或配置?
任何帮助或指示将不胜感激!
java.lang.ClassNotFoundException:jakarta.enterprise.inject.spi.el.ELAwareBeanManager
此类是自 CDI 4.1(Jakarta EE 11,Faces 4.1 也是其中的一部分)以来从核心 CDI API 拆分为补充 CDI API 工件:
jakarta.cdi.el
。 另请参阅 CDI 4.1 规范部分“主要变更”:
CDI 4.1 不再指定与 Jakarta EE 的集成。现在,这将在 Jakarta EE 平台、Web Profile 和 Core Profile 规范中进行指定。
中的统一 EL 集成 API 已被弃用,相关方法已添加到专用接口BeanManager
,该接口存在于新的补充 API 工件中:ELAwareBeanManager
。jakarta.enterprise:jakarta.enterprise.cdi-el-api
因此,换句话说,如果您的目标是 Tomcat 等准系统 servlet 容器,而不是真正的 Jakarta EE 兼容服务器,则以下 CDI 4.1 API 依赖项
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
</dependency>
需要扩展如下:
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-el-api</artifactId>
<version>4.1.0</version>
</dependency>
您的具体情况的替代方案是简单地将现有的
weld-servlet-shaded
依赖项从 5.x 升级到 6.x,因为它已经传递(!)包含正确的 API 类。目前仅处于测试阶段:
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>6.0.0.Beta4</version>
</dependency>
请注意,传递包含意味着您可以安全地从
pom.xml
中删除 CDI API 和 CDI-EL API 依赖项,以保持简单。
与具体问题无关,我看到您还有一个“提供的”Jakarta EE 10 依赖项。当您的目标是准系统 servlet 容器而不是经过认证的 Jakarta EE 运行时时,这并不完全正确。请参考以下链接如何正确整理