`
yefeishan
  • 浏览: 108742 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

Spring加载applicationContext.xml文件

阅读更多

ApplicationContext加载可以通过编码的方式,也可以通过配置的方式

在Web应用中,目录有两种加载方式,

ContextLoaderListener和ContextLoaderServlet,这两者在功能上完全相同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,另一个基于Servlet接口实现。

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
或者
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
这样配置的话,窗口会自动加载/WEB-INF/applicationContext.xml初始化实例

如果要指定配置文件位置可以通过context-param加以指定
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
配置完以后就可以通过
WebApplicationContextUtils.getWebApplicationContext
方法在web应用中获取ApplicationContext引用

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics