title: Get request attributes and store them for later use anywhere in the application
body: <p>I am using Spring MVC and I would like to get some parameters from the request headers and store them an an object that is available anywhere in the application during the current request. Imagine a an application wide metadata class.</p>
  
  <p>I would like to avoid using the RequestContextHolder because other parts of the application below the controller shouldn't care that the values came from the request. It should just be available. </p>
  
  <p>I also thought of using a request scoped bean, but then I would have to wire it up everywhere I wanted to use it. I which goes against "just having it available" </p>
  
  <p>Any info on where to start would be great.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This is my idea. I'm worried that when other requests come, they will set the transactionId of the the previous request. </p>
  
  <p>Wire up a request scoped bean:</p>
  
  <pre><code>     &lt;bean id="metaDataContextHolder" class="com.yp.common.context.MetadataContextHolder" scope="request"&gt;
          &lt;aop:scoped-proxy/&gt;
       &lt;/bean&gt;
  </code></pre>
  
  <p>Here is the request scoped bean</p>
  
  <pre><code>public class MetadataContextHolder {
  
      private static String transactionId;
      //removed other properties for brevity 
  
      public static String getTransactionId() {
          return transactionId;
      }
  
      public void setTransactionId(String transactionId) {
          this.transactionId = transactionId;
      } 
  }
  </code></pre>
  
  <p>Capture the request headers in the filter to be stored for use throughout the application:</p>
  
  <pre><code>public class RequestMetatDataFilter implements Filter
  {
  
          @Autowired
          MetadataContextHolder metaDataHolder;
  
          @Override
          public void init(FilterConfig arg0) throws ServletException
          {
  
          }
  
          /**
           * This filter will add common request metatdata to the MetaDataContextHolder
           */
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse response, FilterChain filerChain) throws IOException, ServletException
          {
                  HttpServletRequest request = ((HttpServletRequest)servletRequest);
                  String transactionId = request.getHeader("transactionId");
                  metaDataHolder.setTransactionId(transactionId);
                  ...
                  //store other values
                  ...
  
                  filerChain.doFilter(request, response);
  
          }
  
          @Override
          public void destroy() {
              // TODO Auto-generated method stub
  
          }
  }
  </code></pre>
  
  <p>Then the metadata can be accessed anywhere without having to wire the metadata bean again like:</p>
  
  <pre><code>MetaDataContextHolder.getTransactionId();
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>As I suspected, the static property "transactionId" in MetadataContextHolder is updated by every request. </p>
  
  <p>Is it possible to achieve what I am trying to do? </p>