如何扩展spring存储库bean,保留现有的dsl查询方法

如何扩展spring存储库bean,保留现有的dsl查询方法,spring,spring-boot,jpa,spring-data-jpa,spring-data,Spring,Spring Boot,Jpa,Spring Data Jpa,Spring Data,在我当前的实现中,我使用了spring存储库bean,它是在Dependent jar中定义的。比如说,这是我在Dependent jar中定义的ModelRepository @Repository public interface ModelRepository extends CustomModelRepository{ Optional<Model> findByModelId(String id); } 我的问题是,由于模块升级问题,我如何在不在依赖代码中添

在我当前的实现中,我使用了spring存储库bean,它是在Dependent jar中定义的。比如说,这是我在Dependent jar中定义的
ModelRepository

@Repository
public interface ModelRepository extends CustomModelRepository{
  Optional<Model> findByModelId(String id);     
}

我的问题是,由于模块升级问题,我如何在不在依赖代码中添加上述查询方法的情况下实现这一点。

存在所谓的服务层,即通常用于应用程序所有业务逻辑的ModelService类。在那里,您可以定义哪个模型实例应该转到存储库方法来处理持久性。
(请注意,可能我没有正确处理您的问题,因此请说明如果是…

因此我最终能够解决问题,避免了灾难性更改

第1步:

创建一个接口,从从属jar扩展主存储库bean,并创建所需的dsl查询方法。该存储库现在拥有所有
ModelRepository
的dsl方法

@Repository
public interface WrapperModelRepository extends ModelRepository{
  List<Model> findByAnotherModelId(String id);    
}

现在,除非您忘记在您的
@EnableJpaRepositories
存储库中提到这个(您当前的包装器实现)包,否则我们所需的bean将被创建,以便spring为您创建存储库。

如何创建和扩展ModelRepository的接口?@SimonMartinelli,是的,我想到了这一点,并尝试了这种方法。但是,
CustomModelRepostory
中的dsl查询方法当时无法识别。是的,我知道这一点,但问题是它已经在Dependent jar(第三方jar)中定义,我不想再这样做,只是为了添加一个单独的自定义查询,该查询将专门用于我的应用程序中。因此,我正在寻找扩展现有spring数据jpa响应的方法。
@Repository
public interface WrapperModelRepository extends ModelRepository{
  List<Model> findByAnotherModelId(String id);    
}
@Component
public class WrapperModelRepositoryImpl implments WrapperModelRepository {

  private CustomModelRepositoryImpl customModelRepository;

  public WrapperModelRepositoryImpl(CustomModelRepositoryImpl repository){
    this.customModelRepository = repository;
  }

  public Model exactQueryMethodNameDeclaredInCustomModelRepository(){
    return customModelRepository.exactQueryMethodNameDeclaredInCustomModelRepository()
  }
}