日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring初始化:org.springframework.we...ContextLoaderListener的作用

發布時間:2025/3/12 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring初始化:org.springframework.we...ContextLoaderListener的作用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring初始化:org.springframework.web.context.ContextLoaderListener的作用

在web.xml種這樣配置

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

ContextLoaderListener的作用就是啟動Web容器時,自動裝配
ApplicationContext.xml的配置信息。因為它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啟動容器時,就會默認執行它實現的方法。

applicationContext的默認的路徑是”/WEB-INF/applicationContext.xml。也可以在web.xml中配置該文件的其他位置,配置如下:

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value> </context-param>

以下是詳解

org.springframework.web.context.ContextLoaderListener類實現了javax.servlet.ServletContextListener接口。ServletContextListener接口能夠監聽ServletContext對象的生命周期,因為每個web應用僅有一個ServletContext對象,故實際上該接口監聽的是整個web應用。

實現該接口的類在web.xml中作為監聽器配置后,當web應用啟動后,會觸發ServletContextEvent事件,調用ContextLoaderListener的contextInitialized(ServletContextEvent sce)方法。

ContextLoaderListener通過一個ContextLoader對象來初始化Spring容器。在contextInitialized方法中調用contextLoader.initWebApplicationContext(event.getServletContext())。

ContextLoader類的initWebApplicationContext方法即可返回一個WebApplicationContext對象context。并通過servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context)將WebApplicationContext對象放置在ServletContext對象中。initWebApplicationContext方法通過調用以下方法實例化并設置WebApplicationContext對象。

因此可以通過WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)獲取WebApplicationContext。內部實現是通過servletContext對象查找該對象,屬性名為WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE。

這篇博文也算是對自己有很多幫助,希望對你有些幫助

總結

以上是生活随笔為你收集整理的Spring初始化:org.springframework.we...ContextLoaderListener的作用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。