当前位置: 首页 > 知识库问答 >
问题:

考虑在配置中定义一个名为“entityManagerFactory”的bean

田兴旺
2023-03-14

我从以下链接下载了示例代码:https://spring.io/guides/gs/accessing-data-mysql/我更改了应用程序。属性文件如下:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

之后,我转到源文件夹并运行以下命令:mvn clean spring boot:run发生此错误:

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

Description:

Field userRepository in hello.MainController required a bean named 'entityManagerFactory' that could not be found.


Action:

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

[WARNING]
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#26e0c680' 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)#26e0c680': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
        at com.dac.vn.Application.main(Application.java:10)
        ... 6 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#26e0c680' 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)#26e0c680': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.658 s
[INFO] Finished at: 2017-02-28T16:43:48+07:00
[INFO] Final Memory: 39M/331M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.0.RELEASE:run (default-cli) on project spring-web-test: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#26e0c680' 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)#26e0c680': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

请帮我解决这个问题

共有2个答案

胡越
2023-03-14

经过大量研究,我终于发现了这个问题。。我忘了在pom中添加hibernate核心和hibernate entitymanager。添加这些之后,我的问题就解决了

傅献
2023-03-14

也许示例代码不完整,或者您在教程中犯了一些错误。

来自与Spring Boot相关的Spring IO文档:

77.6使用自定义EntityManagerFactory

要完全控制EntityManagerFactory的配置,需要添加一个名为“EntityManagerFactory”的@Bean。Spring Boot auto configuration根据该类型bean的存在关闭其实体管理器。

 类似资料:
  • 角色JAVA 任务JAVA 使用者JAVA RoleRepository.java TaskRepository。JAVA 用户库 登录注册应用程序。JAVA application.properties http://maven.apache.org/xsd/maven-4.0.0.xsd" 波姆。xml [错误]测试运行:1,失败:0,错误:1,跳过:0,耗时:2.941秒

  • 尝试启动Spring Boot应用程序时,我收到以下错误: 注意:仅当我将自动连线注释置于IDeviceRepository IDeviceRepository上方时,才会发生此错误 。java类 我还没有让持久化到数据库-但是正在创建实体 这是项目的目录结构: com。美国广播公司。初始化。应用JAVA com.abc.controller.ontroller.java com。美国广播公司。服

  • 我正在Spring中开发一个应用程序,使用Tomcat、Mysql5、Java8。。。问题是,由于“required bean‘entityManagerFactory’not found”问题,我无法部署它。我与同事一起开发了这个项目,但他们可以完美地部署它,即使我在Spring工具套件中复制粘贴相同的项目。怎么可能呢?错误: 这是我的pom.xml 这是我的存储库 这是我的控制器 应用属性 实

  • 尝试运行我的Spring启动应用程序,我的编译器说;“_worldcom.example.hello字段回购。UserService需要一个名为entityManagerFactory的bean,但找不到。 注入点有以下注释:-@org.springframework.beans.factory.annotation.自动生成(必需=true)" Spring启动的新手,所以我不太确定为什么我的项

  • 我只是在学习如何通过spring上传文档。所以,在进行这项工作时,我面临着上述问题。由于有许多问题与同一个问题相关,我参考了它们并尝试了所有方法,但没有一个问题得到纠正。事先,我不需要为我的存储库接口添加@Repository,也不需要在我的应用程序类中添加@EnableJpaRepositories,因为我已经使用了正确的包序列。如需更多参考,请参阅https://dzone.com/artic

  • 感谢您的帮助! 我有一个spring boot应用程序要连接到couchbase,详细信息如下 > 存储库接口 控制器类 Couchbase配置 当我尝试启动应用程序时,它会抛出以下错误,我尝试在这里和外面搜索许多文章,但它们没有帮助 任何帮助都将不胜感激,否则情况似乎很好。