I'm using springBootVersion = '2.5.4'
and trying to autowire the following component:
@Component
@RequestScope
public class TokenDecodingService {
@Autowired
HttpServletRequest httpServletRequest;
/* Some logic with request */
}
into my controller
@RestController
@Slf4j
//@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DrupalEnhancedController {
@Autowired
TokenDecodingService tokenDecodingService;
....
}
During debugging I see that I get No Scope registered for scope name 'request'
exception. The same problem goes for each web-aware scope.
First, I thought that I was using the wrong application context, but I've figured out that I'm using AnnotationConfigReactiveWebServerApplicationContext
and this context should accept web-aware scopes of beans/components.
Also I tried to define this logic inside a bean like this:
@Bean
@RequestScope
public TokenDecodingService tokenDecodingService() {
return new TokenDecodingService();
}
and tried to define context listener manually:
@Bean
public RequestContextListener requestContextListener(){
return new RequestContextListener();
}
But unfortunatelly I didn't make any progress this way. I think that the root cause here is the absence of web-aware contexts, so how can I enable them?
CodePudding user response:
You've mentioned AnnotationConfigReactiveWebServerApplicationContext
, so I assume you have a reactive web application. There's no such thing as "request scope" or even HttpServletRequest
in a reactive application, since in a general case such application is not servlet-based.
You might want to inject the reactive ServerHttpRequest
into your controller method instead:
@RestController
public class DrupalEnhancedController {
@GetMapping("/someurl")
public Mono<SomeResponse> get(ServerHttpRequest request) { ... }
}