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

时间:2019-08-01 14:20:24

标签: java spring spring-boot dependency-injection spring-bean

基于Gophers Slack,我尝试使用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.

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

1 个答案:

答案 0 :(得分:6)

您引用的答案有所不同:它在声明的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 ..

对于@RequestScope的bean,理论上不应使用泛型创建任何问题,因为该bean是在每个请求时创建的,并且不能重用,但是我认为@Bean的泛型功能无法实现这种差异,因此考虑一般情况(单作用域),在这种情况下,必须进行完美匹配才能避免类型安全和一致性问题。 It could interest you


编辑后:

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

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

因此,我们的想法是声明一个原型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;
    }

}

在控制器中注入@Configuration bean requestFactory

private RequestFactory requestFactory; // Change

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

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

@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);
}

现在测试,它看起来可以工作。