title: How to check for Request Scope availability in Spring?
body: <p>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.</p>
  
  <p>The application in question is a web app, but there are some JMX triggers and scheduled tasks (i.e. Quartz) that also trigger invocations.</p>
  
  <p>E.g.</p>
  
  <pre><code>/**
   * 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
          }
      }
  }
  </code></pre>
  
  <p>Assuming <code>myRequestScopedBean</code> is request scoped.</p>
  
  <p>I know this can be done with a <code>try</code>-<code>catch</code> around the invocation of <code>myRequestScopedBean</code>, e.g.:</p>
  
  <pre><code>/**
   * 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
          }
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Many thanks!</p>