WebApplicationContext : org.springframework.web.context.ContextLoaderListener作用

来源:互联网 发布:c4d mac 百度云 编辑:程序博客网 时间:2024/04/29 23:55
如果您想要在自己所定义的Servlet类别中使用Spring的容器功能,则也可以使用 org.springframework.web.context.ContextLoaderListener,例如在web.xml中使用< listener>标签加以定义:
...
<listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
...

ContextLoaderListener预设会读取applicationContext.xml,您可以指定自己的定义档,只要在<context-param>中指定"contextConfigLocation"参数,例如:
...
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/beans-config.xml,
           → /WEB-INF/demo-service.xml</param-value>
</context-param>
...

接着您可以在自定义的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的设定,例如:
...
<servlet>
    <servlet-name>contextLoader</servlet-name>
    <servlet-class>
      org.springframework.web.context.ContextLoaderServlet
    </servlet-class>  
    <load-on-startup>1</load-on-startup>
</servlet>
...

综合以上的叙述,撰写一个简单的范例来示范完整的组态方式,假设您撰写了一个简单的Servlet程式,作用为提供简单的报时功能,如下所示:
  • TimeServlet.java
package onlyfun.caterpillar;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.
support.WebApplicationContextUtils;

public class TimeServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
WebApplicationContext ctx =
WebApplicationContextUtils.
getRequiredWebApplicationContext(
this.getServletContext());

PrintWriter out = res.getWriter();
out.println(ctx.getBean("dateBean"));
}
}

这个Servlet中使用了WebApplicationContext来尝试取得dateBean,您可以在web.xml中定义ContextLoaderListener与Bean定义档的位置,例如:
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
→ http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans-config.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>time</servlet-name>
<servlet-class>
onlyfun.caterpillar.TimeServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>time</servlet-name>
<url-pattern>/time.do</url-pattern>
</servlet-mapping>
</web-app>

在<context-param>的"contextConfigLocation"属性中,设定了Bean定义档的位置与名称,Bean定义档的内容如下所示:
  • beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="dateBean" class="java.util.Date" singleton="false"/>
</beans>

您可以连接至TimeServlet,结果会显示连接上的时间。
原创粉丝点击