[英]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.
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")); }
} }
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> { }
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-pluginI'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:得到教训:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.