上下文——webApplicationContext 與servletContext

來源:互聯網
上載者:User

標籤:spring   tar   環境   export   xtu   blog   研究   介面   ice   

1.WebApplicationContext的研究

        ApplicationContext是spring的核心,Context通常解釋為上下文環境,用“容器”來表述更容易理解一些,ApplicationContext則是“應用的容器了”了。

     spring把bean放在這個容器中,在需要的時候,用getBean()方法取出,在web應用中,會用到webApplicationContext,繼承自ApplicationContext

    在web.xml初始化WebApplicationContext:

 1    <context-param> 2  3            <param-name>contextConfigLocation</param-name> 4  5            <param-value>/WEB-INF/applicationContext.xml</param-value> 6  7   </context-param> 8  9 <listener>10 11      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>12 13 </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)

1 WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext())       2 DataSource ds=    ctx.getBean(DataSource.class);3  jRExportService.setDataSource(ds)

 

這裡得到了spring 的webApplicationContext ,spring的bean都放在裡面,然後直接getBean就可以得到了

上下文——webApplicationContext 與servletContext

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.