Spring Boot启动报错:ErrorPageRegistrarBeanPostProcessor Bean重复定义
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类,与自动配置冲突 - 自定义了同名的
errorPageRegistrarBeanPostProcessorBean
若存在上述情况,移除重复配置即可解决问题。
内容的提问来源于stack exchange,提问作者PEREZ MONSIVAIS JOSE DE JESUS

