Spring在web.xml中的配置詳細(xì)介紹
前言
在實(shí)際項(xiàng)目中spring的配置文件applicationcontext.xml是通過spring提供的加載機(jī)制自動(dòng)加載到容器中。在web項(xiàng)目中,配置文件加載到web容器中進(jìn)行解析。目前,spring提供了兩種加載器,以供web容器的加載:一種是ContextLoaderListener,另一種是ContextLoaderServlet。這兩種在功能上完全相同,只是前一種是基于Servlet2.3版本中新引入的Listener接口實(shí)現(xiàn),而后一種是基于Servlet接口實(shí)現(xiàn),以下是這兩種加載器在web.xml中的配置應(yīng)用:
ContextLoaderListener
listener>
listener-class>org.springframework.context.ContextLoaderListener/listener-class>
/listener>
ContextLoaderServlet
servlet>
servlet-name>context/servlet-name>
servlet-class>org.springframework.context.ContextLoaderServlet/servlet-class>
load-on-startup>1/load-on-startup>
/servlet>
通過上面的配置,web容器會(huì)自動(dòng)加載applicationcontext.xml初始化。
如果需要指定配置文件的位置,可通過context-param加以指定:
context-param>
param-name>contextConfigLocation/param-name>
param-value>/WEB-INF/myApplicationContext.xml/param-value>
/context-param>
之后,可以通過WebApplicationContextUtils.getWebApplicationContext方法在web應(yīng)用中獲取applicationcontext的引用。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:- spring web.xml指定配置文件過程解析
- Spring中基于XML的AOP配置詳解
- SpringBoot集成JmsTemplate(隊(duì)列模式和主題模式)及xml和JavaConfig配置詳解
- spring通過導(dǎo)入jar包和配置xml文件啟動(dòng)的步驟詳解
- Spring boot AOP通過XML配置文件聲明的方法
- SpringBoot之logback-spring.xml不生效的解決方法
- SpringBoot通過yml和xml文件配置日志輸出方法
- 詳解spring applicationContext.xml 配置文件
- SpringBoot返回json和xml的示例代碼
- spring mvc 讀取xml文件數(shù)據(jù)庫(kù)配置參數(shù)的方法
- spring接口通過配置支持返回多種格式(xml,json,html,excel)
- spring使用OXM進(jìn)行對(duì)象XML映射解析
- spring是如何解析xml配置文件中的占位符