繁体   English   中英

使用 Spring Boot 在侦听器中调用用 @RequestScope 注释的类

[英]Calling a class annotated with @RequestScope inside a Listener using Spring boot

org.springframework.beans.factory.BeanCreationException:创建名为“scopedTarget.movi​​mentacaoEntradaRadarNotaBuilderImpl”的bean时出错:当前线程的范围“请求”未激活; 如果您打算从单例中引用它,请考虑为此 bean 定义一个范围代理; 嵌套异常是 java.lang.IllegalStateException: No thread-bound request found: 您是指实际 Web 请求之外的请求属性,还是在原始接收线程之外处理请求? 如果您实际上是在 Web 请求中操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet/DispatcherPortlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。

我需要在不删除 @RequestScope 的情况下解决问题,这仅适用于这种情况,而不适用于整个项目。

@Component
@RequestScope
public class BuilderImplementation implements BuilderInterface {

    @Override
    public void build(){

    }

}

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MovimentacaoEntradaQueueStorageListener {

    private final @NonNull BuilderInterface builderInterface;

    public MessageStatus listen() {

        builderInterface.build();
    }
}

根据( springs 文档,三个范围“请求”、“会话”和“全局会话”只有在有网络感知应用程序上下文时才有可能:

The scopes that are described in the following paragraphs are only 
available if you are using a web-aware Spring ApplicationContext 
implementation (such as XmlWebApplicationContext). If you try using these 
next scopes with regular Spring IoC containers such as the XmlBeanFactory 
or ClassPathXmlApplicationContext, you will get an IllegalStateException 
complaining about an unknown bean scope.

按照官方 spring 文档的指导来解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM