没有类型为AbstractRepository的限定bean <! - ? - >'available:期望的单个匹配bean但找到2

时间:2017-08-30 10:27:22

标签: java spring rest spring-mvc spring-boot

我使用spring-boot来实现一些rest-endpoint。我实现了一个AbstractRepository和一个带有@Autowired的AbstractEnpoint。然后两者都被真正的2个端点扩展:ClientEndpoint和HospitalReservationListEndpoint。 但是当我尝试在端点中调用存储库中的方法时,我在浏览器中收到以下错误:

"org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.parcaune.reservationmanagerservices.core.AbstractRepository<?>' available: expected single matching bean but found 2: clientRepository,hospitalReservationListRepository"

我的课程:

我的抽象存储库

/*
@NoRepositoryBean
Annotation to exclude repository interfaces from being picked up
and thus in consequence getting an instance being created
*/

@NoRepositoryBean
public interface AbstractRepository<T> extends JpaRepository<T, String> 
{
}

我的AbstractEndpoint

public abstract class AbstractEndpoint<T> {

  @Autowired
  protected AbstractRepository<T> repository;

  @GET
  @Path("/ping")
  @Produces(MediaType.TEXT_PLAIN)
  public String ping() {
    return "Endpoint works!";
  }

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public List<T> getAll() {
    List<T> list = new ArrayList<>();
    list = repository.findAll();
    return list;
  }
}

我的客户 - 类:

public interface ClientRepository extends AbstractRepository<Client> {
}

@Component
@Path("/clients")
public class ClientEndpoint extends AbstractEndpoint<Client> {
}

我的医院 - 课程

public interface HospitalReservationListRepository extends 
AbstractRepository<HospitalReservationList> {
}

@Component
@Path("/hospitalreservationlist")
public class HospitalReservationListEndpoint extends 
AbstractEndpoint<HospitalReservationList> {
}

1 个答案:

答案 0 :(得分:0)

简单的方法是将Repository添加为Generic参数,如下所示:

SheetA

我希望有更好的方法来做到这一点,如果你能找到一个请在这里评论。