6

记录 Spring Boot2 启动报错The bean ‘beanNameViewResolver‘, defined in class path...

 3 years ago
source link: https://guisu.blog.csdn.net/article/details/107459739
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

记录 Spring Boot2 启动报错The bean ‘beanNameViewResolver‘, defined in class path resource 解决过程

original.png
hguisu 2020-07-22 17:07:36 articleReadEyes.png 2054
分类专栏: SpringBoot

在必问里面回答一个网友的问题:https://biwen.csdn.net/question/4664

一、问题现象


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2020-07-19 10:47:22.557 ERROR 7412 --- [      main] o.s.b.d.LoggingFailureAnalysisReporter  :

***************************

APPLICATION FAILED TO START

***************************

Description:

The bean 'beanNameViewResolver', defined in class path resource [cn/afterturn/easypoi/configuration/EasyPoiAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

上面错误已经提示我们在application.ym/application.properties添加如下配置就可以解决。
spring.main.allow-bean-definition-overriding=true
spring.main.allow-bean-definition-overriding设置为true,表示后发现的bean会覆盖之前相同名称的bean。
但是添加这个配置无效,启动仍然提示上面错误。

二、问题分析和原因


1、怀疑spring没有读取到spring.main.allow-bean-definition-overriding

把这个参数spring.main.allow-bean-definition-overriding放到启动参数里面,然后启动看看:

20200721143448860.png

然后启动spring boot报错信息,数据源配置错误:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

format,png

这已经说明spring已经正确读取到spring.main.allow-bean-definition-overriding。初步怀疑spring没有读取带application.yml 配置。

2、debug数据源配置信息

1)、在启动参数添加spring.datasource.url

format,png

2)、同时断点debug数据源配置,

format,png

可以看到数据源配置信息,在启动参数里面spring.datasource.url可以读到,但spring.datasource.usernamespring.datasource.password没有读取到。

format,png

明确问题原因:spring无法读取application.yml配置。

由于没有注意springboot启动的提示信息:No active profile set, 但在application.yml已经指明sprin.profiles.active=test. 这就已经说明了spring没有正确读取到application.yml配置。

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hndWlzdQ==,size_16,color_FFFFFF,t_70

3、分析无法读取application.yml文件的原因

1)springboot编译后默认把application.yml文件是放在target/classes目录下。

查看编译后的目录结构, 结果application.yml是在BOOT-INF/classes目录下,导致在idea启动时候,spring无法正常读取到application.yml.

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hndWlzdQ==,size_16,color_FFFFFF,t_70

2)、最后检查maven编译设置,原来是在pom.xm指定了编译路径。

format,png

三、问题解决


注释掉这些信息就可以在IDEA下正常启动:

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hndWlzdQ==,size_16,color_FFFFFF,t_70

spring boot使用spring-boot maven插件打包,会把resource的文件放到BOOT-INF\classes下。所以不必设置编译resources项。

解压打包后20200721143012626.png 的jar包就可以看到resources的源文件都在BOOT-INF\classes目录下。

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hndWlzdQ==,size_16,color_FFFFFF,t_70

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK