我正在使用 Java Spring Boot 開發一個基本應用程式,但遇到了這個錯誤:
這是錯誤訊息:
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.
我的主要課程是這樣的:
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 類是這樣的:
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> { }我的 pom.xml 看起來像這樣:
org.springframework.boot spring-boot-maven-plugin<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>
我一直在 Google 上尋找解決方案,但到目前為止一無所獲,所以我的問題是:我不明白如何克服 EntityManagerFactory 錯誤,我是否缺少依賴項?還是應該重構我的代碼?任何解決方案?
非常感謝您的建議。
uj5u.com熱心網友回復:
好吧,17 天后我終于得到了解決方案:
我的問題出在屬性檔案上,出于某種原因,我使用了這一行(我不記得為什么): spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
我只是評論它并解決了問題。
得到教訓:
- 始終記錄您的代碼
- JitterTed 的不和諧是一個很好的答案來源。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/403430.html
標籤:
