0

I have a REST API webservice which injects a singleton bean. This bean needs to know about the HTTPServletRequest. So i have created another bean called RequestServiceImpl which has HTTPServlerRequest injected.

public class RequestServiceImpl implements RequestService
{
    @Autowired
    private HttpServletRequest request;

}

web-context contains bean definition as

<bean id="requestService" class="com.RequestServiceImpl" scope="request">
     <aop:scoped-proxy/>
</bean>

This requestService is injected into another singleton bean. I do see the requestservice bean being injected correctly but the HTTPServletRequest object is always null. I am not sure where am i going wrong

1 Answers1

-1

HTTPServletRequest is an Interface. You cannot have instance of interface. You may inject class that implements HTTPServletRequest and that is HttpServletRequestWrapper. You could have your reference implementation as well if you want.

This answer will help you further.. Spring Dependency injection for interfaces

Community
  • 1
  • 1
Gaurang Patel
  • 4,310
  • 6
  • 27
  • 32
  • Not sure i understand what you mean. Do you mean to inject the request as @Autowired private HttpServletRequestWrapper request; – user2066789 Jun 03 '15 at 13:40
  • Just tried that. it didnt work. request is still null. Any suggestions – user2066789 Jun 09 '15 at 18:05
  • Please provide more code. E.g. In which class exactly you need HttpServletRequest, Controller, Spring configuration file etc. Meanwhile please check both the answers posted for below question and see if it is helpful, http://stackoverflow.com/questions/3320674/spring-how-do-i-inject-an-httpservletrequest-into-a-request-scoped-bean – Gaurang Patel Jun 10 '15 at 07:25