【发布时间】:2021-11-02 09:24:06
【问题描述】:
我有两个不同的配置文件,并希望为此配置文件有两个不同的存储库。但它是一个实体的存储库,只是在查询中具有不同的条件。
我尝试这样做:
这是我的存储库基本接口:
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();
}
当我使用这个存储库时我有课:
@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
如何解决此问题并根据我的活动配置文件使用一个或另一个存储库?
【问题讨论】:
标签: spring-profiles spring-repositories