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

Spring Boot 3整合JSF遇初始化错误,寻求解决方法

Spring Boot 3.0 整合 JSF 启动失败解决方案

问题回顾

尝试搭建Spring Boot 3.0 + JSF(Jakarta Faces 4.0)项目时,启动报错,核心错误为Could not find backup for factory jakarta.faces.context.FacesContextFactory,同时提示无法获取InjectionProvider。

项目配置信息

依赖配置

implementation 'org.springframework.boot:spring-boot-starter-web:3.0.2'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:10.1.5'
implementation 'org.glassfish:jakarta.faces:4.0.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.0.2'

主应用类

@SpringBootApplication
public class ManagementClientApplication implements ServletContextAware {

    public static void main(String[] args) {
        SpringApplication.run(ManagementClientApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean<FacesServlet> servletRegistrationBean() {
        ServletRegistrationBean<FacesServlet> servletRegistrationBean = new ServletRegistrationBean<>(new FacesServlet(), "*.xhtml");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
        servletContext.setInitParameter("jakarta.faces.FACELETS_SKIP_COMMENTS", "true");
    }
}

faces-config.xml

<?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/web-facesconfig_2_2.xsd"
    version="2.2">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

</faces-config>

测试XHTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>

<h:body>
    <h:form>
        <h:outputText id="greeting" value="Hello world !" />
    </h:form>
</h:body>
</html>

核心错误日志

2023-02-02T17:21:11.133+01:00 ERROR 6312 --- [           main] jakarta.faces                            : Unable to obtain InjectionProvider from init time FacesContext. Does this container implement the Mojarra Injection SPI?
2023-02-02T17:21:11.150+01:00 ERROR 6312 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Servlet.init() for servlet [facesServlet] threw exception

java.lang.IllegalStateException: Could not find backup for factory jakarta.faces.context.FacesContextFactory. 
    at jakarta.faces.FactoryFinderInstance.notNullFactory(FactoryFinderInstance.java:497) ~[jakarta.faces-4.0.1.jar:4.0.1]

解决步骤

1. 更新faces-config.xml至Jakarta EE 9+规范

当前使用Jakarta Faces 4.0,旧的Java EE命名空间和版本会导致不兼容,替换为以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="https://jakarta.ee/xml/ns/jakartaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_4_0.xsd"
              version="4.0">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

</faces-config>

2. 添加Jakarta Injection API依赖

Tomcat作为轻量级Servlet容器,不自带Jakarta Injection实现,需要手动添加依赖:

implementation 'jakarta.inject:jakarta.inject-api:2.0.1'

3. 调整FacesServlet注册方式

避免直接实例化FacesServlet,改为通过类名注册,让Spring管理实例化流程:

@Bean
public ServletRegistrationBean<FacesServlet> facesServletRegistration() {
    ServletRegistrationBean<FacesServlet> registration = new ServletRegistrationBean<>(FacesServlet.class, "*.xhtml");
    registration.setLoadOnStartup(1);
    return registration;
}

4. 确认资源目录结构

确保测试XHTML文件放在src/main/webapp根目录下(如index.xhtml),启动后访问http://localhost:8080/index.xhtml即可看到页面。

5. 处理潜在依赖冲突(可选)

如果出现Tomcat相关依赖版本冲突,可显式排除Spring Boot自带的Tomcat依赖,重新指定版本:

implementation 'org.springframework.boot:spring-boot-starter-web:3.0.2' {
    exclude group: 'org.apache.tomcat.embed', module: 'tomcat-embed-core'
    exclude group: 'org.apache.tomcat.embed', module: 'tomcat-embed-websocket'
}
implementation 'org.apache.tomcat.embed:tomcat-embed-core:10.1.5'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:10.1.5'
implementation 'org.apache.tomcat.embed:tomcat-embed-websocket:10.1.5'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 11:01:29