gpt4 book ai didi

java - 使用 Spring Boot Autowiring 任何接口(interface)

转载 作者:行者123 更新时间:2023-12-01 17:26:24 25 4
gpt4 key购买 nike

我想要 Autowiring 一个在组件中实现的类。这是界面的一部分:

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GenericResourceHandlerService<T extends ResourceRequest, A extends ResultType, B extends ResourceService<T, A>> {

private final B service;

public Response get(String x) {
various checks(x, service.getType());

B res = service.get(x);

if (res!= null) {
return Response.status(Response.Status.OK).entity(res).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
}

然后,实现 ResourceService 的类将如下所示:

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TypeOneService implements EntityService<CityRequest, City> {

private final Repository repository;

@Override
public ResultType getType() {
return ResultType.TYPE_ONE;
}

@Timed
public TYPE_ONE get(String resource) {
return repository.get(resource);
}

}

界面本身如下所示:

public interface EntityService<T extends EntityRequest, A extends ReturnableEntity> {

ResourceType getResourceType();

A get(String resource);

}

现在,我有一组 Controller 尝试 Autowiring GenericResourceHandlerService 并调用它的 get 方法。看起来像这样:

public class TypeOneController {

private final TypeOneService typeOneService;

private final GenericResourceHandlerService<TypeOneRequest, TypeOne, TypeOneService> genericResourceHandlerService;

public Response getListItemByResource(
String resource
) {
return genericResourceHandlerService.get(resource);
}
}

或者这个:

public class TypTwoController {

private final TypeTwoService typeTwoService;

private final GenericResourceHandlerService<TypeTwoRequest, TypeTwo, TypeTwoService> genericResourceHandlerService;

public Response getListItemByResource(
String resource
) {
return genericResourceHandlerService.get(resource);
}
}

这可以编译,但是当应用程序启动时,我收到以下错误消息:

Parameter 0 of constructor in path.GenericResourceHandlerService required a single bean, but 2 were found:
- typeOneSerivce: defined in file [C:\Path\TypeOneService.class]
- typeTwoService: defined in file [C:\Path\TypeTwoService.class]

我认为这是因为 Spring Boot 无法确定要为 Autowiring 提供服务。我想做的事情可能吗?

最佳答案

Spring 尝试注入(inject)一个 bean 来解决 GenericResourceHandlerService.service但服务具有类型 B,并且 B 扩展了 ResourceService。 spring 发现有 2 个 bean 实现了这个接口(interface),所以不知道要 Autowiring 哪个实现..

您可以输入@Qualifier现场service但我想你会失去这种类型的通用性 GenericResourceHandlerService

也许最好的方法是让 Controller 通过 GenericResourceHandlerService 中的实现并让它作为一个简单的pojo..而不是spring bean(所以删除 @Service 上的 GenericResourceHandlerService

像这样

public class TypeOneController {

@Autowired
private final TypeOneService typeOneService;

private final GenericResourceHandlerService<TypeOneRequest, TypeOne, TypeOneService> genericResourceHandlerService = new GenericResourceHandlerService(typeOneService);

public Response getListItemByResource(
String resource
) {
return genericResourceHandlerService.get(resource);
}

}

关于java - 使用 Spring Boot Autowiring 任何接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61204203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com