所以我已經學習了幾個星期的 Spring。我正在嘗試制作一個涉及控制器 -> 服務 -> 存盤庫 -> 資料庫模式的簡單專案。
我開始遇到這個問題,但找不到解決方案。我在網上偶然發現了一些類似的問題,同樣的錯誤,但沒有一個給出我的解決方案,在我的專案中一切似乎都很正常。
這是輸出錯誤:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field repository in com.Library.services.LibraryService 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.Library;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EntityScan("com.Library.services")
@EnableJpaRepositories("com.Library.repositories")
public class LibraryApplication {
public static void main(String[] args) {
SpringApplication.run(LibraryApplication.class, args);
}
}
模型:
package com.Library.models.dtos;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
@Getter
@Setter
@Entity
public class BookCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
private String Name;
}
存盤庫:
package com.Library.repositories;
import com.Library.models.dtos.BookCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookCategoryRepository extends JpaRepository<BookCategory,Long> {
List<BookCategory> findAll();
}
服務:
package com.Library.services;
import com.Library.models.dtos.BookCategory;
import com.Library.repositories.BookCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LibraryService {
@Autowired
BookCategoryRepository repository;
public List<BookCategory> getAllCategories() {
return repository.findAll();
}
}
控制器:
package com.Library.controllers;
import com.Library.models.dtos.BookCategory;
import com.Library.services.LibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
public class LibraryController{
@Autowired
LibraryService libraryService;
public List<BookCategory> getAllCategories() {
return libraryService.getAllCategories();
}
}
可能是什么問題?
uj5u.com熱心網友回復:
這個 bean 定義通常由 Spring Boot Auto-Configuration 自動提供。spring 參考手冊解釋了如何診斷此類問題:
Spring Boot 自動配置盡最大努力“做正確的事”,但有時事情會失敗,而且很難說出原因。
在任何 Spring Boot ApplicationContext 中都有一個非常有用的 ConditionEvaluationReport。如果啟用 DEBUG 日志輸出,您可以看到它。如果您使用 spring-boot-actuator(請參閱 Actuator 章節),還有一個條件端點以 JSON 格式呈現報告。使用該端點來除錯應用程式,并查看 Spring Boot 在運行時添加了哪些功能(哪些還沒有添加)。
通過查看源代碼和 Javadoc 可以回答更多問題。閱讀代碼時,請記住以下經驗法則:
- 查找呼叫的類
*AutoConfiguration并閱讀它們的來源。請特別注意@Conditional*注釋以了解它們啟用了哪些功能以及何時啟用。添加--debug到命令列或系統屬性-Ddebug以在控制臺上獲取在您的應用程式中做出的所有自動配置決策的日志。在啟用了執行器的正在運行的應用程式中,查看條件端點(/actuator/conditions或 JMX 等效項)以獲取相同的資訊。
在您的情況下,一個簡單的全文搜索發現 Hibernate 由 自動配置org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,宣告如下:
@AutoConfiguration(after = { DataSourceAutoConfiguration.class })
@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, EntityManager.class, SessionImplementor.class })
@EnableConfigurationProperties(JpaProperties.class)
@Import(HibernateJpaConfiguration.class)
public class HibernateJpaAutoConfiguration {
}
從 ConditionalOnClass 注釋中可以看出,僅當您的類路徑包含LocalContainerEntityManagerFactoryBean來自 spring-orm-jpa、EntityManagerJPA 規范和SessionImplementorhibernate jar 的類時,才會應用此配置。
最有可能的是,您缺少其中一個 JAR 檔案(maven 依賴項),或者其中一個檔案的版本錯誤。ConditionEvaluationReport 應該告訴您要檢查的包名和準確的包名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/451116.html
