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