Sobes.tech
Junior — Middle

Is Scope Request possible in the context of GenericApplicationContext in Spring?

sobes.tech AI

Answer from AI

In Spring, GenericApplicationContext is a versatile application context that supports registration and management of beans.

By default, GenericApplicationContext supports Singleton and Prototype scopes. Request scope is typically used in web applications for beans that live for the duration of a single HTTP request.

To use Request Scope with GenericApplicationContext, you need to:

  • Manually register RequestScope, as GenericApplicationContext does not have built-in support for web-specific scopes.
  • This is done by registering RequestScope in the context, for example:
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerScope("request", new RequestScope());

However, for full support of Request Scope, AnnotationConfigWebApplicationContext or other specialized contexts integrated with the web environment are usually used.

Thus, technically, Request Scope is possible in GenericApplicationContext, but it requires additional configuration and is not a standard scenario.

Is Scope Request possible in the context of… - sobes.tech