簡體   English   中英

應用程序初始化期間未創建 Web 感知范圍的組件

[英]Component of web-aware scopes are not created during app initialization

我正在使用springBootVersion = '2.5.4'並嘗試自動裝配以下組件:

@Component
@RequestScope
public class TokenDecodingService {
   
   @Autowired
   HttpServletRequest httpServletRequest;
/* Some logic with request */
}

進入我的 controller

@RestController
@Slf4j
//@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DrupalEnhancedController {

    @Autowired
    TokenDecodingService tokenDecodingService;

    ....

}

在調試過程中,我看到No Scope registered for scope name 'request' 每個網絡感知 scope 都會遇到同樣的問題。

首先,我認為我使用了錯誤的應用程序上下文,但我發現我正在使用AnnotationConfigReactiveWebServerApplicationContext並且此上下文應該接受 bean/組件的 Web 感知范圍。

我也嘗試在這樣的 bean 中定義這個邏輯:

    @Bean
    @RequestScope
    public TokenDecodingService tokenDecodingService() {
        return new TokenDecodingService();
    }

並嘗試手動定義上下文偵聽器:

@Bean 
public RequestContextListener requestContextListener(){
    return new RequestContextListener();
}

但不幸的是,我沒有通過這種方式取得任何進展。 我認為這里的根本原因是缺乏網絡感知上下文,那么我該如何啟用它們呢?

您提到了AnnotationConfigReactiveWebServerApplicationContext ,所以我假設您有一個反應式 web 應用程序。 在響應式應用程序中沒有“請求范圍”甚至HttpServletRequest這樣的東西,因為在一般情況下,這樣的應用程序不是基於 servlet 的。

您可能希望將反應式ServerHttpRequest注入到您的 controller 方法中:

@RestController
public class DrupalEnhancedController {

    @GetMapping("/someurl")
    public Mono<SomeResponse> get(ServerHttpRequest request) { ... }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM