No thread-bound request found: Are you referring to request attributes outside of an actual web requ

这篇具有很好参考价值的文章主要介绍了No thread-bound request found: Are you referring to request attributes outside of an actual web requ。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

错误描述

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.

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.
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1537) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1284) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:42010) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) [spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) [spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4759) [catalina.jar:8.5.85]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5229) [catalina.jar:8.5.85]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [catalina.jar:8.5.85]
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753) [catalina.jar:8.5.85]
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:727) [catalina.jar:8.5.85]
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695) [catalina.jar:8.5.85]
	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:673) [catalina.jar:8.5.85]
	at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1881) [catalina.jar:8.5.85]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_131]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_131]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_131]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
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.
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	... 29 more


最后的
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.
	at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41) ~[spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:346) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	... 29 more

出错场景

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

/**
 * 张俊杰 2023年05月06日14:21:21
 */
public class TraceIdUtil {

    private static TraceIdGenerator traceIdGenerator;



    public static String getTraceId() {
        return traceIdGenerator.getTraceId();
    }

    public static void setTraceIdGenerator(TraceIdGenerator traceIdGenerator) {
        TraceIdUtil.traceIdGenerator = traceIdGenerator;
    }

}

import cn.hutool.core.lang.UUID;

/**
 * 张俊杰 2023年05月06日14:21:21
 */
public class TraceIdGenerator {

    private String traceId = UUID.randomUUID().toString();


    public String getTraceId() {
        return this.traceId;
    }

}

    <bean id="traceIdGenerator" class="com.iph.hiring.headcount.util.TraceIdGenerator" scope="request">
<!--       之前是没加下面这个东西,就报错了, 添加个 <aop:scoped-proxy/> 就不报错了. -->
<!--         <aop:scoped-proxy/> -->
    </bean>
    <bean id="traceIdUtil" class="com.iph.hiring.headcount.util.TraceIdUtil">
        <property name="traceIdGenerator" ref="traceIdGenerator"/>
    </bean>

解决思路

参考: https://blog.csdn.net/qq_36951116/article/details/79121887
request bean那边配置添加个 aop:scoped-proxy/ 标签文章来源地址https://www.toymoban.com/news/detail-717447.html

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

到了这里,关于No thread-bound request found: Are you referring to request attributes outside of an actual web requ的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • sparkbug解决:Exception in thread “main“ java.lang.UnsupportedOperationException: No Encoder found for

    报错: 问题原因: Spark SQL中的 Dataset 类需要一个编码器来将JVM对象序列化为内部的Spark SQL格式。而对于复杂类型(比如 Row 或者自定义类),Spark 需要隐式的 Encoder ,代码因为缺少这样的编码器,所以会导致 UnsupportedOperationException 异常。 附上代码 问题解决: 添加 Encoders.kr

    2024年02月04日
    浏览(44)
  • 解决 Could not write request: no suitable HttpMessageConverter found for request type [java.lang.Long]

    业务服务通过RestTemplate调用文件上传服务。( java.version1.8/java.version spring.cloud.versionHoxton.SR12/spring.cloud.version spring.cloud.alibaba.version2.2.9.RELEASE/spring.cloud.alibaba.version spring.boot.version2.3.12.RELEASE/spring.boot.version ) 由于restTemplate中引入了FormHttpMessageConverter消息转换器,在调用过程中

    2024年02月04日
    浏览(60)
  • FileUploadException: the request was rejected because no multipart boundary was found

    前端使用 form 提交文件到后端。 使用 jquery/axios/fetch 或其他HTTP客户端程序发送HTTP请求,但是后端(Spring框架)报错如下: 我是因为设置了 \\\"Content-Type\\\": \\\"multipart/form-data\\\" 这样的头,才会失败。 这个请求头的格式样例(见https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type):

    2024年02月10日
    浏览(51)
  • 超详细干货解决Exception in thread “main“ java.lang.ClassNotFoundException及No suitable driver found for jdbc

    最近一直被eclipse的WindowBuilder和java继jdbc连接Mysql的问题困扰,终于摸索出对我有用的解决方法,希望这对你也有所帮助! 首先,当你在eclipse等IDE编译器里Run数据库连接后,出现Exception in thread \\\"main\\\" java.lang.ClassNotFoundException的结果报错,我本人后面出现的还有illegal一串字符,

    2024年02月06日
    浏览(62)
  • fileupload.FileUploadException: the request was rejected because no multipart boundary was found

    前端小程序调用服务端的文件上传接口,有一台安卓机上传一直报错: 报错日志: {\\\"statusCode\\\": 500, \\\"header\\\": {\\\"Date\\\": \\\"Wed, 29 Mar 2023 06:45:39 GMT\\\", \\\"Content-Language\\\": \\\"en\\\", \\\"Server\\\": \\\"nginx\\\", \\\"Content-Type\\\": \\\"text/html;charset=utf-8\\\", \\\"Transfer-Encoding\\\": \\\"chunked\\\", \\\"Connection\\\": \\\"keep-alive\\\", \\\"protocol\\\": \\\"http/1.1\\\"}, \\\"data

    2024年02月03日
    浏览(65)
  • STS中maven项目打包报错: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题的解决方案

    这个报错的意思是在这个环境中没有编译环境,即不存在javac.exe, 然后说一下JRE和JDK的区别: JRE(Java Runtime Environment): 1、JRE 是 Java 运行时环境,用于在计算机上运行 Java 程序。 2、包含 Java 虚拟机(JVM)和 Java 核心类库,以及运行 Java 程序所需的其他支持文件。 3、JRE 通常

    2024年03月28日
    浏览(37)
  • 已解决IndexError: positional indexers are out-of-bounds

    已解决IndexError: positional indexers are out-of-bounds 粉丝群里面的一个小伙伴遇到问题跑来私信我,想用pandas,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下

    2024年02月05日
    浏览(50)
  • C - Bound Found

    Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: \\\"But I want to use feet, not meters!\\\"). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We\\\'ll not go into details, but researchers fou

    2024年02月08日
    浏览(36)
  • 解决:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin 命令行mvn打包的时候报错:No compiler is provided in this environment. Perhaps you are running on a JRE

    1.mvn clean install -DskipTests; idea terminal 打包报错(用idea右边maven插件打包是没有问题的)。 [ INFO ] BUILD FAILURE [ INFO ] ------------------------------------------------------------------------ [ INFO ] Total time:  1.095 s [ INFO ] Finished at: 2022-03-30T03:40:49+08:00 [ INFO ] --------------------------------------------------

    2024年02月08日
    浏览(59)
  • Invalid bound statement (not found)

    目录 一、遇到的问题 二、分析思路 1、映射文件 2、测试类 三、解决方案 前几日,有个工作不久的同事找我帮他解决一个 Mybatis 的问题。他写了一个增删改查,但是在启动程序的时候报错:Invalid bound statement (not found) 。他试图解决该异常,花了一个小时还是没有解决,所以

    2024年02月01日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包