You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署JSF应用到Tomcat 9时遇Unable to find CDI BeanManager异常求助

JSF部署Tomcat报错Unable to find CDI BeanManager的原因与解决方法

错误原因

  • Tomcat是轻量级Servlet容器,本身不带CDI(上下文与依赖注入)运行时环境,但JSF 2.3及以后版本默认依赖CDI来管理托管Bean、EL表达式依赖注入等核心功能
  • JSF启动时会自动查找CDI规范定义的BeanManager实例,Tomcat里没默认提供这个实例,所以直接抛出异常
  • 要是你的项目里已经放了beans.xml但没引入CDI实现的依赖包,容器也没法识别并初始化CDI环境,同样会触发这个错误

解决方法

1. 给项目添加CDI实现依赖(修改pom.xml)

Tomcat需要手动引入CDI的实现库,常用的有Weld和OpenWebBeans,选一个就行:

用Weld的话,在pom.xml的<dependencies>里加:

<!-- Weld CDI核心依赖,适配Tomcat 9 -->
<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-core</artifactId>
    <version>4.0.3.Final</version> <!-- 版本可按需调整,确保兼容Tomcat 9 -->
</dependency>

用OpenWebBeans的话,依赖配置是:

<dependency>
    <groupId>org.apache.openwebbeans</groupId>
    <artifactId>openwebbeans-web</artifactId>
    <version>2.0.27</version>
</dependency>
<dependency>
    <groupId>org.apache.openwebbeans</groupId>
    <artifactId>openwebbeans-el22</artifactId>
    <version>2.0.27</version>
</dependency>

2. 确保beans.xml配置正确

把beans.xml放在项目的WEB-INF目录下,内容至少要激活CDI扫描:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       version="2.0" bean-discovery-mode="all">
</beans>

bean-discovery-mode="all"会自动扫描项目里所有类作为CDI Bean,适合新手快速配置。

3. 在web.xml里添加CDI监听器

用Weld的话,加这个监听器让它在Tomcat启动时初始化CDI环境:

<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<!-- 可选:开启自动扫描,不用手动指定Bean -->
<context-param>
    <param-name>org.jboss.weld.environment.servlet.scanning</param-name>
    <param-value>true</param-value>
</context-param>

用OpenWebBeans的话,监听器配置换成:

<listener>
    <listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>
<listener>
    <listener-class>org.apache.webbeans.servlet.WebBeansServletContextListener</listener-class>
</listener>

4. 检查faces-config.xml的JSF版本

确保faces-config.xml里声明的JSF版本是2.3及以上,且没禁用CDI:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
              version="2.3">
    <!-- 你的其他JSF配置 -->
</faces-config>

注意别加javax.faces.DISABLE_FACELET_EL_RESOLVER这类会禁用CDI的参数。

内容的提问来源于stack exchange,提问作者Python_user

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:55:09