测试Spring-Boot官网程序踩到坑了

2019-01-02 21:54发布

问题:

下午【抄写】Spring-Boot官网的程序,结果,遇到问题了,查询了很久,一直没有找到解决方法。
请园友帮帮我呗。

官网程序:
Accessing JPA Data with REST
https://spring.io/guides/gs/accessing-data-rest

从其Github下载到本地,导入Eclipse中,运行发生异常:
https://github.com/spring-guides/gs-accessing-data-rest
注意,导入其中的complete文件夹下的内容。

错误提示信息如下:
2018-10-30 20:34:14.382 INFO 7296 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-10-30 20:34:14.401 INFO 7296 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-30 20:34:14.475 ERROR 7296 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


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

Description:

Parameter 0 of constructor in org.springframework.data.rest.webmvc.ProfileController required a bean named 'entityManagerFactory' that could not be found.

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

更多错误信息(上面的储物信息之前):
2018-10-30 20:34:14.379 WARN 7296 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'profileController' defined in URL [jar:file:/D:/ws/apache-maven-3.5.4/repository/org/springframework/data/spring-data-rest-webmvc/3.0.10.RELEASE/spring-data-rest-webmvc-3.0.10.RELEASE.jar!/org/springframework/data/rest/webmvc/ProfileController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositoryRestConfiguration' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'repositoryRestConfiguration' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositories' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': Cannot create inner bean '(inner bean)#38a1c423' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#38a1c423': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

要怎么解决呢?
有的说是JPA和Hibernate冲突,删掉仓库里的Hibernate就可以了,做了,不可以。
还有说在Application里面添加@ComponentScan(...)注解,可是,也不行。

听说Spring-Boot的坑比较多,没想到第一天就遇到一个这么大的!

说明,我对Spring及Spring Boot属于初学。

HELP!

回答1:

刚刚百度了一下,搜索:spring-boot 没有加载我的Repository接口

打开下面的链接:√
https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-configuration-spring-boot

使用其中的 最佳解法 修改自己的Application中的代码:添加三条注解
@ComponentScan({"hello"})
@EntityScan("hello")
@EnableJpaRepositories("hello")

截图:

前面解决问题时 在 @SpringBootApplication 后面添加的 (exclude = DataSourceAutoConfiguration.class) 也 删除 了。

再次运行可得:和Spring官网的示例差不多了,只是,多了profile的链接

一瞬间,整个人轻松了好多好多啊!
对了,还不知道原因,哪位高手还请赐教一二啊?



回答2:

entityManagerFactory
注入失败,看一下entityManagerFactory那边的代码



回答3:

这不是 提示了 entityManagerFactory 需要这么一个类么。。这个不是自己写的,那就是第三方jar包里面的,看看你引用的jar包对不对。或者是你引用的jar包中,有重复版本,导致冲突



回答4:

把你maven仓库中得/D:/ws/apache-maven-3.5.4/repository/org/springframework/data/spring-data-rest-webmvc删掉,重新maven下载



标签:

收藏的人(0)