Spring中配置ContextLoaderListener方式
Spring中配置ContextLoaderListener
web.xml:
<!-- contextConfigLocation參數(shù)用來指定Spring的配置文件 needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-persist-*.xml</param-value>
</context-param>
<!-- 配置spring核心監(jiān)聽器 Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>ContextLoaderListener的作用是什么?
ContextLoaderListener的作用就是啟動(dòng)Web容器時(shí),讀取在contextConfigLocation中定義的xml文件,自動(dòng)裝配ApplicationContext的配置信息,并產(chǎn)生WebApplicationContext對象,然后將這個(gè)對象放置在ServletContext的屬性里,這樣我們只要得到Servlet就可以得到WebApplicationContext對象,并利用這個(gè)對象訪問spring容器管理的bean。
簡單來說,就是上面這段配置為項(xiàng)目提供了spring支持,初始化了Ioc容器。
web.xml 中 ContextLoaderListener 是否可以不配置?
- 如果只有 Spring mvc 的一個(gè) Servlet,listener 可以不用。
- 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必須在 listener 里加載。
- 一般 Dao, Service 的 Spring 配置都會在 listener 里加載,因?yàn)榭赡軙诙鄠€(gè) Servlet 里用到,而且父子 Context 間存在可見性問題,所以,為了防止重復(fù)加載需要在 listener 中進(jìn)行加載。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring+Junit4進(jìn)行接口測試實(shí)例代碼
這篇文章主要介紹了Spring+Junit4進(jìn)行接口測試實(shí)例代碼,涉及pom.xml、bean的配置,以及接口測試代碼等相關(guān)內(nèi)容,小編覺得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01
詳解SpringCloud服務(wù)認(rèn)證(JWT)
本篇文章主要介紹了SpringCloud服務(wù)認(rèn)證(JWT),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
IDEA下Servlet可能出現(xiàn)404的一些情況
相信有很多小伙伴遇到報(bào)錯(cuò)都不知道怎么處理,今天特地整理了這篇文章,文中對IDEA下Servlet可能出現(xiàn)404的一些情況作了詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
Spring Boot thymeleaf模板引擎的使用詳解
這篇文章主要介紹了Spring Boot thymeleaf模板引擎的使用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
RepeatSubmit若依框架如何防止表單重復(fù)提交注解
若依框架中的@RepeatSubmit注解用于防止表單重復(fù)提交,通過在控制器方法上添加該注解,并在前端頁面和JavaScript代碼中實(shí)現(xiàn)雙重校驗(yàn),可以確保同一用戶在短時(shí)間內(nèi)不會重復(fù)提交相同的表單2024-11-11

