我正在使用springBootVersion = '2.5.4'并嘗試自動裝配以下組件:
@Component
@RequestScope
public class TokenDecodingService {
@Autowired
HttpServletRequest httpServletRequest;
/* Some logic with request */
}
進入我的控制器
@RestController
@Slf4j
//@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DrupalEnhancedController {
@Autowired
TokenDecodingService tokenDecodingService;
....
}
在除錯程序中,我看到出現No Scope registered for scope name 'request'例外。每個網路感知范圍都存在同樣的問題。
首先,我認為我使用了錯誤的應用程式背景關系,但我發現我正在使用AnnotationConfigReactiveWebServerApplicationContext并且這個背景關系應該接受 bean/組件的網路感知范圍。
我也嘗試在一個 bean 中定義這個邏輯,如下所示:
@Bean
@RequestScope
public TokenDecodingService tokenDecodingService() {
return new TokenDecodingService();
}
并嘗試手動定義背景關系偵聽器:
@Bean
public RequestContextListener requestContextListener(){
return new RequestContextListener();
}
但不幸的是,我沒有通過這種方式取得任何進展。我認為這里的根本原因是缺少網路感知背景關系,那么我該如何啟用它們呢?
uj5u.com熱心網友回復:
您已經提到了AnnotationConfigReactiveWebServerApplicationContext,所以我假設您有一個回應式 Web 應用程式。沒有“請求范圍”這樣的東西,甚至HttpServletRequest在反應式應用程式中也沒有,因為在一般情況下,此類應用程式不是基于 servlet 的。
您可能希望將反應ServerHttpRequest注入到您的控制器方法中:
@RestController
public class DrupalEnhancedController {
@GetMapping("/someurl")
public Mono<SomeResponse> get(ServerHttpRequest request) { ... }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365170.html
