国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

WebApplicationContextUtils原理

2019-11-08 20:11:20
字體:
來源:轉載
供稿:網友

最重要的就是servletContext對象。這是最基本的運行環境,保存了最基本的數據。所有的web運行中需要的參數都保存在這里,自然也就包括sPRing運行需要的WebapplicationContext對象

當 Web 應用集成 Spring 容器后,代表 Spring 容器的WebApplicationContext對象將以

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 為鍵存放在ServletContext的屬性列表中。您當然可以直接通過以下語句獲取 WebApplicationContext:

WebApplicationContext wac = (WebApplicationContext)servletContext.

getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

但通過位于 org.springframework.web.context.support 包中的WebApplicationContextUtils 工具類獲取 WebApplicationContext 更方便:

WebApplicationContext wac =WebApplicationContextUtils.

getWebApplicationContext(servletContext);

當 ServletContext 屬性列表中不存在 WebApplicationContext 時,getWebApplicationContext() 方法不會拋出異常,它簡單地返回 null。如果后續代碼直接訪問返回的結果將引發一個 NullPointerException 異常,而 WebApplicationContextUtils 另一個 getRequiredWebApplicationContext(ServletContext sc) 方法要求 ServletContext 屬性列表中一定要包含一個有效的 WebApplicationContext 對象,否則馬上拋出一個 IllegalStateException 異常。我們推薦使用后者,因為它能提前發現錯誤的時間,強制開發者搭建好必備的基礎設施。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岱山县| 永仁县| 独山县| 灵武市| 集贤县| 昌都县| 独山县| 门头沟区| 田林县| 浦江县| 大余县| 蒙山县| 内丘县| 蒙自县| 寿阳县| 祥云县| 新源县| 阿拉尔市| 赤峰市| 松滋市| 利津县| 赞皇县| 昆山市| 十堰市| 石渠县| 工布江达县| 文安县| 布拖县| 鄄城县| 临城县| 图们市| 长葛市| 瑞安市| 黄浦区| 钦州市| 措美县| 大悟县| 崇仁县| 磴口县| 蒲江县| 呼玛县|