JAVA认证辅导:SPRING容器外访问SPRING
来源:优易学  2011-11-28 21:40:38   【优易学:中国教育考试门户网】   资料下载   IT书店

  在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");

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训