I'm working on a basic application using Java Spring Boot, I'm stuck on this error:
我正在使用Java Spring Boot开发一个基本的应用程序,我遇到了这个错误:
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:
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:
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:
<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
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? or should I refactor my code? any solutions?
我一直在Google上寻找解决方案,但到目前为止还没有找到任何解决方案,所以我的问题是:我不知道如何克服EntityManager Factory错误,我是不是错过了一个依赖项?或者我应该重构我的代码?有什么解决办法吗?
Thanks a lot for your suggestions.
非常感谢你的建议。
更多回答
Did you try simplifying your pom.xml
file? Seems to me you're importing too many items -- and some of them are already included in the starter package of Spring. See this MySQL sample from Spring Data tutorial.
您是否尝试过简化pom.xml文件?在我看来,您导入的项目太多了--其中一些已经包含在了Spring的入门包中。请参阅这个来自Spring data的MySQL示例教程。
FOr starters your dependencies are a mess. Ditch all the hibernate dependencies. Remove the spring-web
and spring-webmvc
dependencies. On your @SpringBootApplication
annotated class remove @ComponentScan
and @EnableJpaRepositories
. Then restart your application using --debug
to get more information and the stacktrace telling you what is wrong. You probably misconfigured your database.
首先,你的依赖关系是一团糟。抛弃所有的休眠依赖。删除Spring-web和Spring-webmvc依赖项。在您的@SpringBootApplication注释类上,删除@ComponentScan和@EnableJpaRepositories。然后使用--DEBUG重新启动您的应用程序以获取更多信息,并使用堆栈跟踪告诉您哪里出了问题。您可能错误配置了数据库。
thanks a lot, as a matter of fact, searching about this bug I started to add dependencies with no control and still the problem persist. I cleaned up my pom.xml and I will start the app in debug mode, I'll let you know what I get
非常感谢,事实上,在搜索这个错误时,我开始在没有控制的情况下添加依赖项,但问题仍然存在。我清理了我的pom.xml,我将在调试模式下启动应用程序,我会让你知道我得到了什么
优秀答案推荐
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:
吸取的经验教训:
- always document your code
- JitterTed's discord is a great source of answers.
Issue : Consider defining a bean named 'entityManagerFactory' in your configuration.
问题:考虑在您的配置中定义一个名为‘entityManagerFactory’的Bean。
Resolution: After 3 days of struggle I found my problem was due to a property on properties file: spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
解决方案:经过3天的努力,我发现我的问题是由于属性文件中的一个属性:spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Post commented, at least this issue resolved.
I got another issue, which I will update soon.
波斯特评论说,至少这个问题得到了解决。我有另一期,我会很快更新的。
更多回答
it resolve my issue as well. Thanks!
它也解决了我的问题。谢谢!
我是一名优秀的程序员,十分优秀!