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

Spring Boot启动报错:ErrorPageRegistrarBeanPostProcessor Bean重复定义

解决Spring Boot启动时errorPageRegistrarBeanPostProcessor Bean重复定义问题

问题详情

按照Spring Boot官方教程创建Maven项目,核心代码如下:

MyApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

}

执行启动命令 mvn --debug spring-boot:run 时抛出BeanDefinitionOverrideException,核心错误日志:

The bean 'errorPageRegistrarBeanPostProcessor', defined in class path resource [org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.class], could not be registered. A bean with that name has already been defined and overriding is disabled.

项目pom.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>untitled</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.1.0-RC1</version>
    </parent>

    <properties>
        <maven.compiler.source>23</maven.compiler.source>
        <maven.compiler.target>23</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

已执行mvn dependency:tree和mvn dependency:analyze-duplicate排查,未发现重复依赖;不想通过设置spring.main.allow-bean-definition-overriding=true规避问题,寻求根本解决方案。

解决方案

1. 切换到Spring Boot稳定版本

当前使用的4.1.0-RC1是候选发布版本,可能存在未修复的内部Bean定义重复问题。建议降级到最新稳定版(如4.0.9):

修改pom.xml中的parent版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>4.0.9</version> <!-- 替换为最新稳定版 -->
</parent>

执行mvn clean install刷新依赖后重新启动应用。

2. 检查JDK版本兼容性

Spring Boot 4.1.x对JDK23的支持仍处于早期阶段,可能存在兼容性问题。建议切换到长期支持(LTS)版本JDK21:

修改pom.xml中的编译版本配置:

<properties>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

3. 排查自动配置重复加载

检查项目中是否存在以下情况:

  • 多个类标注了@SpringBootApplication注解,导致自动配置重复触发
  • 手动导入了ErrorPageFilterConfiguration类,与自动配置冲突
  • 自定义了同名的errorPageRegistrarBeanPostProcessor Bean

若存在上述情况,移除重复配置即可解决问题。

内容的提问来源于stack exchange,提问作者PEREZ MONSIVAIS JOSE DE JESUS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.02 07:57:30