在JSP或者SERVLET外訪問SPRING也是很常見的.ApplicationContext是Spring的容器環境,通過ApplicationContext對象可以訪問所有配置的bean。
在Web開發開發中,常常需要從JSP或者Servlet或者Action中獲取ApplicationContext對象,這時候,就無法使用new關鍵字通過查找配置文件來實例化ApplicationContext這個對象了。Spring通過WebApplicationContextUtils可以了.
當然首先要在WEB.XML中配置
org.springframework.web.context.ContextLoaderListener
之后就可以訪問了:
WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(getServletContext());
IlogService ilog=(IlogService)wac.getBean("logService");