I have a requirement - where we use the HttpServletRequest request object to set the UserSession Details in this Object.
The UserSession are required to be fetched at various point in the Spring Application as per need and to access this, I Autowired the HttpServletRequest object. I used in multiple locations and it worked like a charm.
But the problem is, when I tried to use it in a Static contect, this request object throws compilation error "non-static variable . . . cannot be referenced from a static context" .
So, when I tried to make the HttpServletRequest as static, this turns out to be null. I am not sure why this happens and require a suitable solution to use in the static methods.
POJO Class
public class POJOClass{
@Autowired
private HttpServletRequest request;
public static String getData(){
UserObject uObj = UtilService.getUser(request);
//Throws Compilation error for request object.
}
*Here UtilService is the class defined from which the UserSession is fetched.