I saw two ways to get a servletContextbean in a service / controller.
1) simple way - just autowiring:
@Autowired
ServletContext servletContext;
2) another way is to implement the interface servletContextAware:
@RestController("/mycontroller")
public myController implements ServletContextAware {
private ServletContext context;
@Override
public void setServletContext(ServletContext context) {
this.context = context;
}
public String getContextPath(){
return context.getContextPath();
}
}
What are the pros and cons of these two? Which one is recommended?
source
share