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