简体   繁体   English

Java Spring 引导:考虑在配置中定义一个名为“entityManagerFactory”的bean

[英]Java Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configuration

I'm working on a basic application using Java Spring Boot, I'm stuck on this error:我正在使用 Java Spring 启动一个基本应用程序,我遇到了这个错误:

This is the error message:这是错误消息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-16 12:44:40.321 ERROR 5698 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.htamayo.sbcrashcourse.SbcrashcourseApplication required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
  1. My main class looks like this:我的主要 class 看起来像这样:

     package com.htamayo.sbcrashcourse; import com.htamayo.sbcrashcourse.lendingengine.domain.model.User; import com.htamayo.sbcrashcourse.lendingengine.domain.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication @ComponentScan(basePackages = {"com.htamayo.sbcrashcourse.lendingengine"}) @EnableJpaRepositories("com.htamayo.sbcrashcourse.lendingengine.domain.repository") public class SbcrashcourseApplication implements CommandLineRunner { @Autowired private UserRepository userRepository; public static void main(String[] args) {SpringApplication.run(SbcrashcourseApplication.class, args);} @Override public void run(String... args) throws Exception { userRepository.save(new User(1, "John", "B", 27, "Software Developer")); userRepository.save(new User(2, "Peter", "C", 21, "Pilot")); userRepository.save(new User(3, "Henry", "E", 21, "Unemployed")); }

    } }

  2. UserRepository class is this: UserRepository class 是这样的:

     package com.htamayo.sbcrashcourse.lendingengine.domain.repository; import com.htamayo.sbcrashcourse.lendingengine.domain.model.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { }
  3. And my pom.xml looks like this:我的 pom.xml 看起来像这样:

     <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.32.Final</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.5.7.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.5.7.Final</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency>
    org.springframework.boot spring-boot-maven-plugin org.springframework.boot spring-boot-maven-plugin

I've been searching for solutions on Google but so far nothing, so my question is: I don't understand how to overcome the EntityManagerFactory error, am I missing a dependency?我一直在谷歌上寻找解决方案,但到目前为止一无所获,所以我的问题是:我不明白如何克服 EntityManagerFactory 错误,我是否缺少依赖项? or should I refactor my code?还是我应该重构我的代码? any solutions?任何解决方案?

Thanks a lot for your suggestions.非常感谢您的建议。

Well, after 17 days finally I got the solution:好吧,17天后我终于得到了解决方案:

my problem was on properties file, for some reason I used this line (I can't remember why): spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration我的问题出在属性文件上,出于某种原因,我使用了这一行(我不记得为什么): spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

I just commented it and problem solved.我只是评论它并解决了问题。

Lessons learned:得到教训:

  1. always document your code始终记录您的代码
  2. JitterTed's discord is a great source of answers. JitterTed 的 discord 是一个很好的答案来源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot:考虑在配置中定义一个名为“ entityManagerFactory”的bean - Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configuration 考虑在您的配置-Spring 启动中定义一个名为“entityManagerFactory”的 bean - Consider defining a bean named 'entityManagerFactory' in your configuration-Spring boot Spring 数据 JPA - 考虑在您的配置中定义一个名为“entityManagerFactory”的 bean - Spring Data JPA - Consider defining a bean named 'entityManagerFactory' in your configuration 考虑在你的配置中定义一个名为&#39;entityManagerFactory&#39;的bean,升级到spring boot 2后的mssqlserver - Consider defining a bean named 'entityManagerFactory' in your configuration., mssqlserver after upgrade to spring boot 2 麻烦定义一个&#39;bean&#39;,“考虑在你的配置中定义一个名为&#39;entityManagerFactory&#39;的bean” - Troubling defining a 'bean' , "Consider defining a bean named 'entityManagerFactory' in your configuration" 考虑在配置中定义一个名为“ entityManagerFactory”的bean。3 - Consider defining a bean named 'entityManagerFactory' in your configuration.3 考虑在配置SpringBoot中定义一个名为“ entityManagerFactory”的bean - Consider defining a bean named 'entityManagerFactory' in your configuration SpringBoot 考虑定义类型为EntityManagerFactory的bean春季启动 - Consider defining a bean of type EntityManagerFactory Spring boot 考虑在您的配置中定义一个“服务”类型的 bean [Spring boot] - Consider defining a bean of type 'service' in your configuration [Spring boot] 考虑在您的配置中定义一个类型为“Mapper”的 bean [Spring-Boot] - Consider defining a bean of type 'Mapper' in your configuration [Spring-Boot]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM