找不到 Springboot 存储库

问题描述

我有这个错误的问题。 - Springboot 不能注入 JPARepository。

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-19 10:07:59.712 ERROR 6912 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION Failed TO START
***************************

Description:

Field enginePartRepository in cz.fry.bmw.OM.services.OrderService required a bean of type 'cz.fry.bmw.OM.DAO.coreid.EnginePartRepository' 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 of type 'cz.fry.bmw.OM.DAO.coreid.EnginePartRepository' in your configuration.

在我的电脑上一切正常,但如果我想在 Windows 服务器上部署应用程序作为 Windows 服务,WinSW.NET4.exe 应用程序会因此错误而崩溃。

我有两个数据源(同一台服务器上的两个不同的 sql 数据库)第一个数据库工作正常。

我已经尝试过 @EntityScan @ComponentScan 没有成功。 我尝试删除存储库,但此数据库中的每个存储库都抛出此错误

@ComponentScan
@EntityScan("cz.fry.bmw.om.DAO")
@EnableAutoConfiguration
@SpringBootApplication
public class OrderManagementApplication {

[Project structure]1

我正在运行 Java 11、Springboot 2.4.2。和 MSsql

解决方法

您还需要@EnableJpaRepositories

@EnableJpaRepositories(basePackages = {"cz.fry.bmw.om.DAO"}) <---------
@ComponentScan
@EntityScan("cz.fry.bmw.om.DAO")
@EnableAutoConfiguration
@SpringBootApplication
public class OrderManagementApplication {

我注意到的另外两件事。

1)

"cz.fry.bmw.om.DAO"

在您的项目结构中,它看起来像

"cz.fry.bmw.OM.DAO"

2)

@ComponentScan

这里提供根包

@ComponentScan("cz.fry.bmw.OM");