I\'m new with Spring, and I\'m trying to use the @Autowire annotation for the ServletContext with my class attribute:
@Controller
public class ServicesImpl i
You'll probably want to take a look at MockServletContext which can be used in unit tests.
Implement the ServletContextAware interface and Spring will inject it for you
@Controller
public class ServicesImpl implements Services, ServletContextAware{
private ServletContext context;
public void setServletContext(ServletContext servletContext) {
this.context = servletContext;
}
ServletContext
is not a Spring bean and it can, therefore, not be injected unless you implement ServletContextAware.
If one thinks in modules or layers then the servlet context shouldn't be available outside the web module/layer. I suppose your ServicesImpl
forms part of the business or service layer.
If you gave a little more context we might be able to suggest better alternatives.