国产探花免费观看_亚洲丰满少妇自慰呻吟_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 異常。我們推薦使用后者,因為它能提前發現錯誤的時間,強制開發者搭建好必備的基礎設施。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郁南县| 昌都县| 泸溪县| 陆川县| 漳州市| 胶南市| 三都| 万安县| 峨边| 锡林浩特市| 永德县| 石林| 穆棱市| 武宣县| 石棉县| 清苑县| 和平区| 乌鲁木齐市| 丹阳市| 鹤壁市| 舟山市| 保定市| 龙南县| 洱源县| 浮山县| 武冈市| 阳高县| 双辽市| 枣阳市| 玉龙| 萨迦县| 舞钢市| 历史| 高阳县| 延川县| 浦东新区| 天峨县| 红河县| 类乌齐县| 达日县| 马公市|