In Spring MVC with annotation, we mark any POJO with @Controller. In this controller, we can get the WebApplicationContext using the autwired property.
@Controller public class HomePageController { @Autowired ApplicationContext act; @RequestMapping("/*.html") public String handleBasic(){ SimpleDomain sd = (SimpleDomain)act.getBean("sd1"); System.out.println(sd.getFirstProp()); return "hello"; }
But in this approach, we do not have a servlet. User-friendly interface. So is it possible to use the old way to get WebApplicationContext? i.e.
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
How do we get servletContext here?
I do not encounter any compulsion to use the old method; so this question is just out of curiosity to check spring's flexibility. It may also be an interview question.
spring spring-mvc servlets autowired applicationcontext
Kaushik lele
source share