简体   繁体   English

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

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

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.movimentacaoEntradaRadarNotaBuilderImpl': Scope 'request' is not active for the current thread; org.springframework.beans.factory.BeanCreationException:创建名为“scopedTarget.movi​​mentacaoEntradaRadarNotaBuilderImpl”的bean时出错:当前线程的范围“请求”未激活; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton;如果您打算从单例中引用它,请考虑为此 bean 定义一个范围代理; 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?嵌套异常是 java.lang.IllegalStateException: No thread-bound request found: 您是指实际 Web 请求之外的请求属性,还是在原始接收线程之外处理请求? 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.如果您实际上是在 Web 请求中操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet/DispatcherPortlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。

I need something to solve the problem without removing @RequestScope and that is only for this case and not for the whole project.我需要在不删除 @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();
    }
}

According to ( springs documentation the three scopes 'request', 'session', and 'global session' are only possible if there is a web-aware ApplicationContext:根据( 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.

Follow the guidance of the official spring documentation to solve your problem.按照官方 spring 文档的指导来解决您的问题。

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

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