【问题标题】:How to create a bean for JPA Respository extened interface ( working in multiple modules)如何为 JPA Repository 创建一个 bean 扩展接口(在多个模块中工作)
【发布时间】:2021-05-18 04:31:35
【问题描述】:

我使用了 2 个模块:

  1. 通用模块
  2. 用户模块

在 common 模块中,我使用了一个扩展 JPA Respository(例如:commonRepository)的接口来访问数据。 当我只运行公共模块时,它可以正常工作

我只是在POM.XML中的用户模块中添加了公共模块的依赖

在控制器中,我调用 common 模块的服务方法,其中 commonRepository 在 commonService 中自动装配

通用模块代码:

CommonController -> CommonServiceInterface -> CommonServiceImplementation (Autowired of CommonRepository) -> CommonRepository extends JPARespository -> getData()

用户模块代码:

UserController (Autowired of CommonServiceInterface )-> Access the getData() from CommonRespository

错误:

Field commonRepository in com.common.commonServiceImplementationrequired a bean of type 'com.common.repository.CommonRepository' that could not be found.
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

注意: 如果我直接从 UserController 访问 CommonRepository 接口,它可以正常工作:

UserController (Autowired of CommonRepository)-> Access the getData() from CommonRespository

当我通过如下服务层访问时,会引发上述错误

UserController (Autowired of CommonServiceInterface )-> Access the getData() from CommonRespository

源代码在

https://github.com/navee-kumar/CommonRepository

https://github.com/navee-kumar/DependentRepository

【问题讨论】:

    标签: spring-boot spring-mvc jpa spring-data-jpa


    【解决方案1】:

    需要在你的 serviceImpl 中添加 Autowired。

    @Autowired
    private CountryRepository countryRepository;
    
    public SimpleMasterServiceImpl(CountryRepository countryRepository) {
        this.countryRepository = countryRepository;
    }
    
    @Override
    public String getData() {
        return countryRepository.findAllByOrderByCountryName().get(0).getCountryName();
    }
    
    @Override
    public String demoCheck() {
        return "demoCheck....";
    }
    

    【讨论】:

    • 但是我已经在配置文件中添加了 File Name : MasterConfig.java 并且我在 Dependent Configuration File 中导入了该配置文件 File Name : DependentConfig.java
    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2013-11-05
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多