自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

介紹Servlet容器與Context

開發(fā) 后端
本文介紹Servlet容器與Servlet Context,以及介紹強制轉化要使用接口,如果使用實現類會報類型轉換錯誤。

Servlet容器啟動創(chuàng)建了許多對象,如Servlet,filter,listener,spring等等那么如何使用這些對象呢?

下面介紹在Servlet(或者Filter,或者Listener)中使用spring的IOC容器默認情況下Servlet容器創(chuàng)建spring容器對象,注入到Servlet Context中,Servlet Context對象又是注入到session對象中,session對象又是注入到request對象中,request對象又是注入到Servlet對象中,(其實不是很標準的注入,是傳參數,或者對屬性直接付值)。層層依賴可以得到spring容器對象。

  1. WebApplicationContext webApplicationContext = WebApplicationContextUtils.
    getWebApplicationContext(request.getSession().getServletContext());  

所以可以直接在Servlet Context取出Web Application Context對象:

  1. WebApplicationContext webApplicationContext = (WebApplicationContext) 
    servletContext.getAttribute(WebApplicationContext.
    ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  

事實上Web Application ContextUtils.getWebApplicationContext方法就是使用上面的代碼實現的,建議使用上面上面的靜態(tài)方法

注意:在使用web Application Context.getBean("ServiceName")的時候,前面強制轉化要使用接口,如果使用實現類會報類型轉換錯誤。如:

  1. LUserService userService = (LUserService) 
    webApplicationContext.getBean("userService"); 

【編輯推薦】

  1. Servlet Context的范圍
  2. 淺析Servlet 3.0 API的概念
  3. Servlet注釋與部署描述符
  4. Java Servlet和Servlet 3.0的新特性
  5. Servlet和JSP潛在的隱患
責任編輯:佚名 來源: 中國IT實驗室
相關推薦

2009-07-09 13:39:52

Servlet Con

2009-07-09 11:27:59

Servlet容器

2009-07-08 16:19:41

Servlet ConServlet Con

2009-07-08 10:12:04

Servlet Con

2022-03-31 15:17:04

JavaSocketServlet容器

2009-07-09 13:04:37

Servlet接口

2022-03-10 08:24:17

Docker容器SaaS

2013-09-02 09:18:59

2009-07-07 15:24:49

Http Servle

2009-07-07 09:51:49

Servlet實例

2009-07-09 15:30:53

Servlet容器會話跟蹤

2009-07-08 14:01:47

Servlet容器

2009-07-07 16:23:05

Servlet容器

2009-07-08 14:22:36

Servlet容器匹配過程

2009-07-09 13:23:44

Servlet 2.4

2010-05-27 15:05:23

ServletJava

2009-07-03 11:21:43

Servlet和JSPJSP路徑

2009-07-07 16:05:15

Servlet和Jav

2009-07-07 17:32:31

HTTP Servle

2024-12-06 08:00:00

點贊
收藏

51CTO技術棧公眾號