小编典典

从Java 8函数创建请求范围的Bean

spring-boot

基于此答案,我尝试使用java.util.Function接口配置请求范围Bean

我的配置如下所示:

@Configuration
public class RequestConfig {

    @Bean
    public Function<? extends BaseRequest, RequestWrapper<? extends BaseRequest, ? extends BaseResponse>> requestWrapperFactory() {
        return request -> requestWrapper(request);
    }

    @Bean
    @RequestScope
    public RequestWrapper<? extends BaseRequest, ? extends BaseResponse> requestWrapper(
            BaseRequest request) {
        RequestWrapper<?, ?> requestWrapper = new RequestWrapper<BaseRequest, BaseResponse>(request);
        return requestWrapper;
    }
}

我尝试像这样使用bean:

@RestController
public class CheckRequestController {

    private final RequestService<CheckRequest, CheckResponse> checkRequestServiceImpl;

    @Autowired
    private Function<CheckRequest, RequestWrapper<CheckRequest, CheckResponse>> requestWrapperFactory;

    public CheckRequestController(
            RequestService<CheckRequest, CheckResponse> checkRequestServiceImpl) {
        super();
        this.checkRequestServiceImpl = checkRequestServiceImpl;
    }

    @PostMapping(value = "/check", consumes = { MediaType.TEXT_XML_VALUE,
            MediaType.MULTIPART_FORM_DATA_VALUE }, produces = MediaType.TEXT_XML_VALUE)
    public ResponseEntity<CheckResponse> checkRequest(
            @RequestBody(required = true) CheckRequest checkRequest) {

        RequestWrapper<CheckRequest, CheckResponse> requestWrapper = requestWrapperFactory
                .apply(checkRequest);
        checkRequestServiceImpl.getResponse(requestWrapper);

        return new ResponseEntity<CheckResponse>(requestWrapper.getResponse(),
                HttpStatus.OK);
    }
}

和这里:

@RestController
public class CancelRequestController {

private final RequestService<CancelRequest, CancelResponse> cancelRequestServiceImpl;

@Autowired
private Function<CancelRequest, RequestWrapper<CancelRequest, CancelResponse>> requestWrapperFactory;

public CancelRequestController(
        RequestService<CancelRequest, CancelResponse> cancelRequestServiceImpl) {
    super();
    this.cancelRequestServiceImpl = cancelRequestServiceImpl;
}

@PostMapping(value = "/cancel", consumes = { MediaType.TEXT_XML_VALUE,
        MediaType.MULTIPART_FORM_DATA_VALUE }, produces = MediaType.TEXT_XML_VALUE)
public ResponseEntity<CancelResponse> CancelRequest(
        @RequestBody(required = true) CancelRequest cancelRequest) {
    RequestWrapper<CancelRequest, CancelResponse> requestWrapper = requestWrapperFactory
            .apply(cancelRequest);
    cancelRequestServiceImpl.getResponse(requestWrapper);
    return new ResponseEntity<CancelResponse>(requestWrapper.getResponse(),
            HttpStatus.OK);
}
}

但是我得到一个例外,就是没有Function定义类型的bean 。

  Field requestWrapperFactory in CheckRequestController required a bean of type 'java.util.Function' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'java.util.Function' in your configuration.

使用泛型类型是否有问题?我怎么了


阅读 405

收藏
2020-05-30

共1个答案

小编典典

您引用的答案有所不同:它在声明的bean和注入的bean中使用完全相同的泛型类型:

@Bean
public Function<String, Thing> thingFactory() {
    return name -> thing(name); // or this::thing
}

和:

@Autowired
private Function<String, Thing> thingFactory;

使用泛型类型是否有问题?我怎么了

是。您要注入具有此签名的bean:

Function<CheckRequest, RequestWrapper<CheckRequest, CheckResponse>> requestWrapperFactory;

但是你用这个签名声明了一个bean:

Function<? extends BaseRequest, RequestWrapper<? extends BaseRequest, ? extends BaseResponse>>

这里 :

@Bean
public Function<? extends BaseRequest, RequestWrapper<? extends BaseRequest, ? extends BaseResponse>> requestWrapperFactory() {
    return request -> requestWrapper(request);
}

bean声明和连接的bean中使用的泛型必须相同,才能在依赖项注入方面进行匹配。

因此,只需在双方声明相同的类型。

所以这意味着没有办法使用泛型来配置bean?因为我也想将bean创建也用于CancelRequest(更新后的答案)。因此,我必须为所有类型的BaseRequest创建一个Bean。

对于@RequestScopebean,从理论上讲,使用泛型不会造成任何问题,因为Bean是在每个请求时创建的,并且不会重用,但是我认为for的泛型功能@Bean没有什么区别,因此考虑一般情况(单作用域)完美匹配是避免类型安全和一致性问题所必需的。


编辑后:

我更新了第一部分以与您的更改保持一致。

现在,您的要求是声明一个函数,该函数将向客户端返回具有由客户端指定的通用类型的原型Bean。
那是可能的。但是,要使其简洁,就不应使用两个bean:一个用于工厂(单例),另一个用于创建RequestWrapper对象(原型)。
由于工厂bean不允许客户端指定通用类型,因此您将不得不执行不希望的取消广播。
您还应该替换为@RequestScope@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)因为请求范围内的Bean不允许像配置类中的singleton和prototypeBean那样可配置。
例如,使用参数或通配符不能很好地工作。

因此,我们的想法是声明一个原型bean,其返回的泛型类型取决于参数和目标。
关于RequestConfig,现在最好将其命名为RequestFactory它的角色。

@Configuration
public class RequestFactory {

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public <T extends BaseRequest, U extends BaseResponse> RequestWrapper<T, U> requestWrapper(
            T request) {
        RequestWrapper<T, U> requestWrapper = new RequestWrapper<>(request);
        return requestWrapper;
    }

}

在控制器中注入@Configurationbean requestFactory

private RequestFactory requestFactory; // Change

public CheckRequestController(
        RequestService<CheckRequest, CheckResponse> checkRequestServiceImpl,
        RequestConfig requestConfig) {
    this.checkRequestServiceImpl = checkRequestServiceImpl;
    this.requestFactory = requestFactory; // Change
}

现在,您可以在需要RequestWrapper时注入所需的原型bean :

@PostMapping(value = "/cancel", consumes = { MediaType.TEXT_XML_VALUE,
        MediaType.MULTIPART_FORM_DATA_VALUE }, produces = MediaType.TEXT_XML_VALUE)
public ResponseEntity<CancelResponse> CancelRequest(
        @RequestBody(required = true) CancelRequest cancelRequest) {
    RequestWrapper<CheckRequest, CheckResponse> requestWrapper = 
               requestFactory.requestWrapper(cancelRequest);
     //...
    return new ResponseEntity<CancelResponse>(requestWrapper.getResponse(),
            HttpStatus.OK);
}

现在进行测试,看起来可以正常工作。

2020-05-30