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

Create request scoped beans from a Java 8 Function

基于this answer我尝试使用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.

使用泛型有问题吗?我做错了什么?

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

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

和:

@Autowired
private Function<String, Thing> thingFactory;

Is there a problem by using generic types? What do I wrong?

是的。你想用这个签名注入一个 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 必须相同才能在依赖注入方面匹配。

所以只要在两边声明相同的类型即可。

so this means there is no way to configure a bean using generics? because I wanted to use the bean creation also for CancelRequest (updated answer). So I have to create a Bean for all types of BaseRequest..

对于 @RequestScope bean,理论上使用泛型不会产生任何问题,因为 bean 是在每次请求时创建的,不会重复使用,但我认为 @Bean 的通用特性不会' t 造成这种差异,因此考虑一般情况(单例范围),在这种情况下需要完美匹配以避免类型安全和一致性问题。 It could interest you


编辑后:

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

现在您的要求是向客户端声明一个函数,该函数 returns 具有客户端指定的通用类型的原型 bean。
这是可能的。但是为了让它整洁,你不应该使用两个 beans:一个用于工厂(单例),另一个用于创建 RequestWrapper 对象(原型)。
由于工厂 bean 不允许客户指定通用类型,因此您将不得不执行不需要的取消转换。
您还应该将 @RequestScope 替换为 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE),因为请求范围的 bean 不允许在配置 class.
中像单例和原型 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
}

现在您可以在需要时注入具有所需 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);
}

现在已经测试过了,它看起来可以工作。