springboot整合jpa问题

问题

Springboot整合JPA后项目启动报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-24 16:10:02.139 ERROR 15952 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'userRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:

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


Process finished with exit code 1

解决

提示很清楚,这个Bean已经注册了,不能被覆盖。可以在配置中增加spring.main.allow-bean-definition-overriding=true允许。

但很明细不能直接增加这句提示就可以解决,原因是存在2个一样名称的bean,首先可以检查自己的类是否有写重复名称了。如果没有就可能是和我一样,依赖导入重复了。

用idea查看pom.xml的依赖图,可以发现

spring-boot-starter-data-jpa内部已经有了spring-boot-starter-data-jdbc

而我在新建springboot项目时,又勾选了jdbc,导致依赖重复。所以只需要删除多余的spring-boot-starter-data-jdbc依赖即可。