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

Spring Boot应用启动失败:Bean定义冲突问题求助

Spring Boot启动失败:Bean定义冲突问题

问题描述

Spring Boot应用无法启动,未修改任何配置类却触发配置类解析异常。日志显示ConflictingBeanDefinitionException:两个不同包路径下的ProductGroupsController类生成了同名bean productGroupsController,导致定义冲突。

错误日志

/usr/bin/env /usr/lib/jvm/jdk-17-oracle-x64/bin/java @/tmp/cp_4p3poytlo5gc9l704f0d0siz2.argfile com.lacertech.lacereats.LacerEatsApplication 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.3)

2023-09-19T10:16:16.647-03:00  INFO 2366827 --- [           main] c.l.lacereats.LacerEatsApplication       : Starting LacerEatsApplication using Java 17.0.8 with PID 2366827 (/home/italo-lacerda/worckspace/LacerEats/lacerEats-BackEnd/target/classes started by italo-lacerda in /home/italo-lacerda/worckspace/LacerEats/lacerEats-BackEnd)
2023-09-19T10:16:16.654-03:00  INFO 2366827 --- [           main] c.l.lacereats.LacerEatsApplication       : No active profile set, falling back to 1 default profile: "default"
2023-09-19T10:16:17.157-03:00  WARN 2366827 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lacertech.lacereats.LacerEatsApplication]
2023-09-19T10:16:17.261-03:00 ERROR 2366827 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lacertech.lacereats.LacerEatsApplication]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:178) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:415) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:287) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:771) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:589) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.3.jar:3.1.3]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.3.jar:3.1.3]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.3.jar:3.1.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.3.jar:3.1.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.3.jar:3.1.3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.3.jar:3.1.3]
        at com.lacertech.lacereats.LacerEatsApplication.main(LacerEatsApplication.java:10) ~[classes/:na]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'productGroupsController' for bean class [com.lacertech.lacereats.registrations.products.controllers.ProductGroupsController] conflicts with existing, non-compatible bean definition of same name and class [com.lacertech.lacereats.registrations.productgroups.controllers.ProductGroupsController]
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:289) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:243) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:164) ~[spring-context-6.0.11.jar:6.0.11]
        ... 13 common frames omitted

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lacertech</groupId>
    <artifactId>lacer-eats</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lacer-eats</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主类代码

package com.lacertech.lacereats;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LacerEatsApplication {

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

}

解决方案

  • 重命名Controller类:直接修改其中一个ProductGroupsController的类名,确保两个类名唯一,比如将com.lacertech.lacereats.registrations.products.controllers.ProductGroupsController改名为ProductsGroupController。
  • 显式指定Bean名称:在其中一个Controller的@RestController注解中自定义bean名称,避免冲突:
    @RestController("productsProductGroupsController")
    public class ProductGroupsController {
        // 业务代码
    }
    
  • 调整包扫描范围:如果其中一个Controller无需被Spring管理,可以在主类的@SpringBootApplication中排除对应类或包:
    @SpringBootApplication(exclude = com.lacertech.lacereats.registrations.products.controllers.ProductGroupsController.class)
    public class LacerEatsApplication {
        public static void main(String[] args) {
            SpringApplication.run(LacerEatsApplication.class, args);
        }
    }
    
    注意:此方式仅适用于不需要该Controller的场景,不推荐作为常规解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 17:19:49