在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");
责任编辑:小草