畅购商城学习日志9.17

发布时间 2023-09-17 20:42:07作者: 六月生

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.changgou.GoodsApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'brandController' for bean class [com.changgou.goods.controller.BrandController] conflicts with existing, non-compatible bean definition of same name and class [com.changgou.controller.BrandController]--畅购商城学习日志

导航:

1.问题描述:

​ 在做P14添加商品辅助数据查询测试启动时,启动GoodsApplication发生错误,报错原因是org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.changgou.GoodsApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'brandController' for bean class [com.changgou.goods.controller.BrandController] conflicts with existing, non-compatible bean definition of same name and class [com.changgou.controller.BrandController]

2.思路经历:

经过搜寻,发现并没有两个BrandController名字的类,然后询问GPT,其回复如下:

是的,如果 com.changgou.controller.BrandController 类位于 target 目录下而不是源代码目录中,那可能会导致错误。一般情况下,target 目录是编译后生成的目录,包含编译后的字节码文件和打包后的可执行文件。**

在 Spring 应用程序中,Spring 容器通过扫描源代码目录中的类来加载和解析配置类,然后根据配置进行相应的初始化和装配。如果你将源代码目录中的类正确地放入了对应的包路径下,那么 Spring 容器应该能够找到并解析这些类。但如果类文件未正确放置,例如在 target 目录中,Spring 容器则无法找到这些类,从而引发错误。

因此,建议你检查项目结构,确保 com.changgou.controller.BrandController 类是在源代码目录中正确的位置,而不是在 target 目录或其他非源代码目录中。如果确实存在问题,将其移至正确的目录,并确保编译时能成功生成相应的字节码文件

接着,我到Maven里clean相关的包选clean,再次运行,搞定。

3.产生原因:

​ 位于 target 目录下而不是源代码目录中,那可能会导致错误。一般情况下,target 目录是编译后生成的目录,包含编译后的字节码文件和打包后的可执行文件。

4.解决方法:

到Maven里clean相关的包选择->生命周期->clean