飞车启动客户端失败:sevlet 加载spring配置

来源:百度文库 编辑:九乡新闻网 时间:2024/05/01 09:02:13

如果您想要在自己所定义的Servlet类别中使用Spring的容器功能,则也可以使用 org.springframework.web.context.ContextLoaderListener,例如在web.xml中使用< listener>标签加以定义:

...


org.springframework.web.context.ContextLoaderListener


...


ContextLoaderListener预设会读取applicationContext.xml,您可以指定自己的定义档,只要在中指定"contextConfigLocation"参数,例如:

...

contextConfigLocation
/WEB-INF/beans-config.xml,
→ /WEB-INF/demo-service.xml


...


接着您可以在自定义的Servlet中使用 org.springframework.web.context.support.WebApplicationContextUtils,从 ServletContext中取得org.springframework.web.context.WebApplicationContext,例如: 

WebApplicationContext ctx = 
WebApplicationContextUtils.
getRequiredWebApplicationContext(
this.getServletContext());


WebApplicationContext实作了ApplicationContext介面,是Spring专为Servlet的Web应用程式设计的 ApplicationContext实作类别,在取得WebApplicationContext之后,您可以利用它来取得Bean定义档中定义的 Bean实例,例如:
Date date = (Date) ctx.getBean("dateBean");


在不支援Listener设定的容器上(例如Servlet 2.2以更早的版本),您可以使用org.springframework.web.context.ContextLoaderServlet来取代上面的ContextLoaderListener的设定,例如:

...

contextLoader

org.springframework.web.context.ContextLoaderServlet
 
1

...