Spring 具有不同配置文件的存储库

Spring Repositories with different profiles

我有两个不同的配置文件,并希望为这些配置文件创建两个不同的存储库。但它是一个实体的存储库,只是在查询中有不同的条件。

我尝试这样做:

这是我的存储库基本界面:

public interface RepositoryBaseInterface extends CrudRepository<MyEntity, Long> {
    List<MyEntity> getEntities();
}

我有两个配置文件存储库:

@Repository
@Profile("profile1)
public interface ProfileRepository extends RepositoryBaseInterface {
    @Query("My query 1")
    List<MyEntity> getEntities();
}

还有一个:

@Repository
@Profile("profile2)
public interface ProfileRepository2 extends RepositoryBaseInterface {
    @Query("My query 2")
    List<MyEntity> getEntities();
}

当我使用这个存储库时我有 class:

@Service
@RequiredArgConstructor
public class UseRepository{

  RepositoryBaseInterface repository;

  public List<MyEntity> getMyEntities() {
     return repository.getEntities();
  }
}

我原以为当我有两个活动配置文件之一时,它将被选为两个 ProfilesRepositories 之一,但我看到只有 RepositoryBaseInterface 在使用,我收到了错误

Error creating bean with name RepositoryBeanInterface
IllegalArgumentException, No property getEntities found for type MyEntity

如何解决此问题并根据我的活动配置文件使用一个或另一个存储库?

我明白我的错误了。我们的基础接口应该没有任何扩展。但它的实施应该。

所以我有

    public interface RepositoryBaseInterface {
       List<MyEntity> getEntities();
    }

还有两个Profile接口:

    @Repository
    @Profile("profile1)
    public interface ProfileRepository extends RepositoryBaseInterface, CrudRepository<MyEntity, Long> {

      @Query("My query 1")
      List<MyEntity> getEntities();
}

    @Repository
    @Profile("profile2)
    public interface ProfileRepository2 extends RepositoryBaseInterface, CrudRepository<MyEntity, Long> {

      @Query("My query 2")
      List<MyEntity> getEntities();
}