首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

web项目如何启动spring容器

2012-08-15 
web项目怎么启动spring容器1. 首先,在web.xml中配置spring的配置文件的位置context-paramparam-nameco

web项目怎么启动spring容器
1. 首先,在web.xml中配置spring的配置文件的位置
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-applicationContext.xml</param-value>
</context-param>
注:context-param中定义的是application范围内的参数,存放在servletcontext中

2. 在web.xml中定义listener
<listener>
    <listener-class>com.test.ApplicationContextListener</listener-class>
</listener>
   ApplicationContextListener是org.springframework.web.context.ContextLoaderListener的子类。ContextLoaderListener实现ServletContextListener,读取contextConfigLocation中定义的xml文件,如果不设置contextConfigLocation的初始参数则默认会读取WEB-INF路径下的 application.xml文件。ContextLoaderListener读取这些XML文件并产生 WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。

热点排行