错误描述

Caused by: 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'traceIdUtil' defined in file [/Users/xxx/IdeaProjects/hiring-headcount-2/hiring-headcount-biz/target/classes/config/spring/local/appcontext-bean.xml]: Cannot resolve reference to bean 'traceIdGenerator' while setting bean property 'traceIdGenerator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'traceIdGenerator': 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
  2. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  3. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  4. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1537) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1284) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  8. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  9. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  10. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  13. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  14. at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  15. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:42010) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  16. at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  17. at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) [spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  18. at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) [spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  19. at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4759) [catalina.jar:8.5.85]
  20. at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5229) [catalina.jar:8.5.85]
  21. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [catalina.jar:8.5.85]
  22. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753) [catalina.jar:8.5.85]
  23. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:727) [catalina.jar:8.5.85]
  24. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695) [catalina.jar:8.5.85]
  25. at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:673) [catalina.jar:8.5.85]
  26. at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1881) [catalina.jar:8.5.85]
  27. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_131]
  28. at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_131]
  29. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
  30. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
  31. at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
  32. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'traceIdGenerator': 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
  33. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  34. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  35. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  36. ... 29 more
  37. 最后的
  38. Caused by: 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
  39. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  40. at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  41. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:346) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  42. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  43. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
  44. ... 29 more

出错场景

Spring工具类静态注入一个Request作用域的bean

  1. /**
  2. * 张俊杰 2023年05月06日14:21:21
  3. */
  4. public class TraceIdUtil {
  5. private static TraceIdGenerator traceIdGenerator;
  6. public static String getTraceId() {
  7. return traceIdGenerator.getTraceId();
  8. }
  9. public static void setTraceIdGenerator(TraceIdGenerator traceIdGenerator) {
  10. TraceIdUtil.traceIdGenerator = traceIdGenerator;
  11. }
  12. }
  1. import cn.hutool.core.lang.UUID;
  2. /**
  3. * 张俊杰 2023年05月06日14:21:21
  4. */
  5. public class TraceIdGenerator {
  6. private String traceId = UUID.randomUUID().toString();
  7. public String getTraceId() {
  8. return this.traceId;
  9. }
  10. }
  1. <bean id="traceIdGenerator" class="com.iph.hiring.headcount.util.TraceIdGenerator" scope="request">
  2. <!-- 之前是没加下面这个东西,就报错了, 添加个 <aop:scoped-proxy/> 就不报错了. -->
  3. <!-- <aop:scoped-proxy/> -->
  4. </bean>
  5. <bean id="traceIdUtil" class="com.iph.hiring.headcount.util.TraceIdUtil">
  6. <property name="traceIdGenerator" ref="traceIdGenerator"/>
  7. </bean>

解决思路

参考: https://blog.csdn.net/qq_36951116/article/details/79121887

request bean那边配置添加个 标签

  1. <bean id="traceIdGenerator" class="com.iph.hiring.headcount.util.TraceIdGenerator" scope="request">
  2. <aop:scoped-proxy/>
  3. </bean>
  4. <bean id="traceIdUtil" class="com.iph.hiring.headcount.util.TraceIdUtil">
  5. <property name="traceIdGenerator" ref="traceIdGenerator"/>
  6. </bean>