错误描述

Caused by: java.lang.IllegalStateException: No threadbound 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/ 标签

    <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>

原文地址:https://blog.csdn.net/qq_41489540/article/details/130533443

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_49487.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注