Search code examples
javaspringrequesthttprequestsingleton-type

Getting request object in a spring singleton bean


I have a spring's singleton bean and i need to access the request object in that bean. I tried Autowiring the HttpServletRequest in the singleton bean :

@Autowired
private HttpServletRequest request;

...

public void setRequest(HttpServletRequest request) {
  this.request = request;
}

public HttpServletRequest getRequest() {
  return request;
}

But than i realized it wont work, because the class is acting as a singleton bean (Object will be created once per bean container)

Then i searched for an alternative, and found a code snipet

 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = requestAttributes.getRequest();

which was supposed to work, but didn't succeeded.

Is there any other way to go through this problem?


Solution

  • To Access HttpServeletRequest Object in Singleton Bean :

    First write a simple filter like below and configure it in web.xml You can make the intercepted URL of your choice below it is configured to /* which will be filtered to all url patterns.

    Create a class Context

    public class Context {  
      private static ThreadLocal<Context> instance = new ThreadLocal<Context>();  
      private HttpServletRequest request;  
      private Context(HttpServletRequest request) {  
           this.request = request;  
      }  
      public static Context getCurrentInstance() {  
           return instance.get();  
      }  
      public static Context newInstance(HttpServletRequest request) {  
           Context context = new Context(request);  
           instance.set(context);  
           return context;  
      }  
      public void release() {  
           instance.remove();  
      }  
      public HttpServletRequest getRequest() {  
           return request;  
      }  
     }  
    

    Create a filter "RequestAccessFilter.java"

    public class RequestAccessFilter implements Filter {  
      @Override  
      public void destroy() {  
           // Nothing Needed            
      }  
      @Override  
      public void doFilter(ServletRequest request, ServletResponse response,  
                FilterChain chain) throws IOException, ServletException {  
           Context context = Context.newInstance((HttpServletRequest) request);  
           try {  
             chain.doFilter(request, response);  
           } finally {  
             context.release();  
           }  
      }  
      @Override  
      public void init(FilterConfig arg0) throws ServletException {  
           // Nothing Needed  
      }  
     }
    

    configure the filter in "web.xml"

     <filter>  
        <filter-name>requestAccessFilter</filter-name>  
        <filter-class>com.myapp.web.filter.RequestAccessFilter</filter-class>  
     </filter>  
     <filter-mapping>  
        <filter-name>requestAccessFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
     </filter-mapping> 
    

    In Your Bean Class you can access

    HttpServletRequest request = Context.getCurrentInstance().getRequest();