I'm trying to setup some code that will behave one way if spring's request scope is available, and another way if said scope is not available.
The application in question is a web app, but there are some JMX triggers and scheduled tasks (i.e. Quartz) that also trigger invocations.
E.g.
/**
* This class is a spring-managed singleton
*/
@Named
class MySingletonBean{
/**
* This bean is always request scoped
*/
@Inject
private MyRequestScopedBean myRequestScopedBean;
/* can be invoked either as part of request handling
or as part of a JMX trigger or scheduled task */
public void someMethod(){
if(/* check to see if request scope is available */){
myRequestScopedBean.invoke();
}else{
//do something else
}
}
}
Assuming myRequestScopedBean
is request scoped.
I know this can be done with a try
-catch
around the invocation of myRequestScopedBean
, e.g.:
/**
* This class is a spring-managed singleton
*/
@Named
class MySingletonBean{
/**
* This bean is always request scoped
*/
@Inject
private MyRequestScopedBean myRequestScopedBean;
/* can be invoked either as part of request handling
or as part of a JMX trigger or scheduled task */
public void someMethod(){
try{
myRequestScopedBean.invoke();
}catch(Exception e){
//do something else
}
}
}
but that seems really clunky, so I'm wondering if anyone knows of an elegant Spring way to interrogate something to see if request-scoped beans are available.
Many thanks!
A request-scoped bean is an object managed by Spring, for which the framework creates a new instance for every HTTP request. The app can use the instance only for the request that created it. Any new HTTP request (from the same or other clients) creates and uses a different instance of the same class (figure 2).
@RequestScope is a specialization of @Scope for a component whose lifecycle is bound to the current web request. Specifically, @RequestScope is a composed annotation that acts as a shortcut for @Scope("request") with the default proxyMode() set to TARGET_CLASS .
When used as a type-level annotation in conjunction with @Component , @Scope indicates the name of a scope to use for instances of the annotated type. When used as a method-level annotation in conjunction with @Bean , @Scope indicates the name of a scope to use for the instance returned from the method.
You can use the if check described here
SPRING - Get current scope
if (RequestContextHolder.getRequestAttributes() != null)
// request thread
instead of catching exceptions. Sometimes that looks like the simplest solution.
You can inject a Provider<MyRequestScopedBean>
and catch the exception when calling the get
method but you should rethink your design. If you feel strongly about it you should probably have two beans with different qualifier
Edit
On second thought, if you are using java configuration, @Scope("prototype")
your @Bean
method and make your decision there, you can get a handle on request context via RequestContextHolder
if available. But I strongly recommend you rethink your design
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With