Java 带有CompletableFuture调用的Spring引导--创建名为';scopedTarget.asyncBean';:范围';请求';

Java 带有CompletableFuture调用的Spring引导--创建名为';scopedTarget.asyncBean';:范围';请求';,java,spring,spring-boot,Java,Spring,Spring Boot,如果有人遇到了这个问题和解决方案,如果spring boot microservice已经启动并运行,那就太好了。我在服务器启动时创建了bean(所有这些bean都是通过@Component/@Service和@requestscope创建的请求范围)。我有一个控制器,在那里它接收到一个http请求,并试图通过CompletableFuture从多个bean/src收集数据。当我尝试使用异步功能从控制器调用时,我得到了错误 @GetMapping("/start") publ

如果有人遇到了这个问题和解决方案,如果spring boot microservice已经启动并运行,那就太好了。我在服务器启动时创建了bean(所有这些bean都是通过@Component/@Service和@requestscope创建的请求范围)。我有一个控制器,在那里它接收到一个http请求,并试图通过CompletableFuture从多个bean/src收集数据。当我尝试使用异步功能从控制器调用时,我得到了错误

@GetMapping("/start")
public String startAsync() throws InterruptedException, ExecutionException {
    System.out.println("Entering into method ");
    
    CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> asyncBean.getStrValue());

    CompletableFuture<Void> future = completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s));

    completableFuture.get();
    
    return "started";
}
@GetMapping(“/start”)
公共字符串startAsync()引发InterruptedException、ExecutionException{
System.out.println(“进入方法”);
CompletableFuture=CompletableFuture.supplyAsync(()->asyncBean.getStrValue());
CompletableFuture=CompletableFuture.thenAccept(s->System.out.println(“计算返回:+s));
completableFuture.get();
返回“已启动”;
}
错误:

021-05-23 20:23:44.355 ERROR 99160 --- [nio-9094-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.concurrent.ExecutionException: org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.asyncBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.] with root cause

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-5.3.7.jar:5.3.7]
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:42) ~[spring-web-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) ~[spring-aop-5.3.7.jar:5.3.7]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:676) ~[spring-aop-5.3.7.jar:5.3.7]
    at com.example.async.demo.ms.async.AsyncBean$$EnhancerBySpringCGLIB$$97833ab7.getStrValue(<generated>) ~[classes/:na]
    at com.example.async.demo.ms.async.AyncController.lambda$0(AyncController.java:38) ~[classes/:na]
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590) ~[na:1.8.0_201]
    at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582) ~[na:1.8.0_201]
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) ~[na:1.8.0_201]
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) ~[na:1.8.0_201]
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) ~[na:1.8.0_201]
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) ~[na:1.8.0_201]
021-05-23 20:23:44.355错误99160---[nio-9094-exec-1]o.a.c.c.c.[/].[dispatcherServlet]:路径[]上下文中Servlet[dispatcherServlet]的Servlet.service()引发异常嵌套异常是java. UTI.CONCURNET.OcjnFraskWork.BeANS.Fask.SpPut.StEnEntAcExvExtExcExc:错误创建bean,名称为“SeopEdvest.Acsicbean”:作用域“请求”对于当前线程不是活动的;如果您打算从一个bean引用它,请考虑为这个bean定义一个作用域代理。singleton;嵌套异常为java.lang.IllegalStateException:未找到线程绑定的请求:您是在实际web请求之外引用请求属性,还是在原始接收线程之外处理请求?如果您实际上在web请求中操作,但仍然收到此消息,则代码可能正在运行DispatcherServlet外部:在这种情况下,使用RequestContextListener或RequestContextFilter公开当前请求。]
java.lang.IllegalStateException:未找到线程绑定请求:您是在实际web请求之外引用请求属性,还是在原始接收线程之外处理请求?如果您实际上在web请求中操作,并且仍然收到此消息,则代码可能在DispatcherServlet之外运行:在这种情况下,使用RequestContextListener或RequestContextFilter公开当前请求。
在org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)~[spring-web-5.3.7.jar:5.3.7]
在org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:42)~[spring-web-5.3.7.jar:5.3.7]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371)~[spring-beans-5.3.7.jar:5.3.7]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)~[spring-beans-5.3.7.jar:5.3.7]
在org.springframework.aop.target.SimpleBanTargetSource.getTarget(SimpleBanTargetSource.java:35)~[spring-aop-5.3.7.jar:5.3.7]
在org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:676)~[spring-aop-5.3.7.jar:5.3.7]
在com.example.async.demo.ms.async.AsyncBean$$EnhancerBySpringCGLIB$$97833ab7.getStrValue()~[classes/:na]
在com.example.async.demo.ms.async.AyncController.lambda$0(AyncController.java:38)~[classes/:na]
在java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)~[na:1.8.0\u 201]
在java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1582)~[na:1.8.0\u 201]
在java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)~[na:1.8.0_201]
在java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)~[na:1.8.0\u 201]
在java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)~[na:1.8.0\u 201]
在java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)~[na:1.8.0201]

有人面临这个问题吗?