使用WebApplicationContext 获取文件内容

来源:互联网 发布:首届全球程序员节直播 编辑:程序博客网 时间:2024/06/07 17:58




1.WebApplicationContext的研究
      ApplicationContext是spring的核心,Context通常解释为上下文环境,用“容器”来表述更容易理解一些,ApplicationContext则是“应用的容器了”了。
     spring把bean放在这个容器中,在需要的时候,用getBean()方法取出,在web应用中,会用到webApplicationContext,继承自ApplicationContext
    在web.xml初始化WebApplicationContext:
   <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
或者用ContextLoaderServlert亦可(加<load-on-startup>1</load-on-startup>)
 
2.ServletContext详解
    ServletContext 是Servlet与Servlet容器之间直接通信的接口,Servlet容器在启动一个web应用时,会为它创建一个ServletContext对 象,每个web应用有唯一的ServletContext对象,同一个web应用的所有Servlet对象共享一个 ServletContext,Servlet对象可以通过它来访问容器中的各种资源
存取共享数据方法:
 setAttribute(String name,Object obj)
getAttribute(String name)


     WebApplicationContext  ctx=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
       
         DataSource ds=    ctx.getBean(DataSource.class);
         jRExportService.setDataSource(ds)
这里得到了spring 的webApplicationContext ,spring的bean都放在里面,然后直接getBean就可以得到了




  WebApplicationContext acx = WebApplicationContextUtils                .getWebApplicationContext(filterConfig.getServletContext());            Resource[] rs = acx.getResources(filterConfig.getInitParameter(PATH));


0 0
原创粉丝点击