---
title: "Spring管理filter和servlet"
author: "jiangwt100"
date: "2011-11-12T16:54:55Z"
tags: []
categories: ["互联网"]
slug: "spring-filter-servlet"
draft: false
lastmod: "2011-11-12T16:55:09Z"
id: 1254975
url: "https://jiangwt100.iteye.com/blog/1254975"
---

在使用spring容器的web应用中，业务对象间的依赖关系都可以用context.xml文件来配置，并且由spring容器来负责依赖对象  的创建。如果要在filter或者servlet中使用spring容器管理业务对象，通常需要使用

 

WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext，然后调用WebApplicationContext.getBean("beanName")来获得对象的引用，这实际上是使用了依赖查找来获得对象，并且在filter或者servlet代码中硬编码了应用对象的bean名字。为了能在filter或者servlet中感知spring中bean，可采用如下步骤来实现：

 

1-  将filter或者servlet作为bean定义在context.xml文件中，和要应用的bean定义放在一起；

 

2-  实现一个filter代理或者servlet代理，该代理用WebApplicationContext来获得在context.xml中定义的filter或者servlet的对象，并将任务委托给context.xml中定义的filter或者servlet

 

3-  在web.xml中用ContextLoaderListener来初始化spring  的context，同时在filter代理或者servlet代理的定义中用初始化参数来定义context.xml中filter或者servlet的bean名字（或者直接受用代理的名称获得相应的filter或者servlet的名称）。

 

4-  在web.xml中定义filter代理或者servlet代理的mapping.

 

利用这种方式就将filter或者servlet和业务对象的依赖关系用spring  来进行管理，并且不用在servlet中硬编码要引用的对象名字。

 

 

 

具体实例如下：

Filter

1\.       在applicationContext.xml中定义filter

 

&lt;bean id="springFilter" class="com.netqin.filter.SpringFilter"&gt;

 

              &lt;property name="name"&gt;

 

                  &lt;value&gt;SpringFilter&lt;/value&gt;

 

              &lt;/property&gt;

 

       &lt;/bean&gt;

 

说明：com.netqin.filter.SpringFilter为实现了javax.servlet.Filter接口的filter

 

2\.       实现filter代理

 

实际上，filter代理不需要我们自己来实现，Spring提供了两种现成的filter代理

 

org.springframework.security.util.FilterToBeanProxy，

 

org.springframework.web.filter.DelegatingFilterProxy，两者只是在web.xml中的配置上略有不同，下面就让我们一起看看如何在web.xml中进行配置。

 

 

 

3\.       配置web.xml

 

?         初始化spring的context

 

因为是使用spring来管理，所以在使用filter前先要初始化spring的context，一般来说配置如下：

 

&lt;context-param&gt;

 

        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

 

        &lt;param-value&gt;

 

            /WEB-INF/applicationContext.xml

 

        &lt;/param-value&gt;

 

    &lt;/context-param&gt;

 

    &lt;listener&gt;

 

        &lt;listener-class&gt;

 

            org.springframework.web.context.ContextLoaderListener

 

        &lt;/listener-class&gt;

 

    &lt;/listener&gt;

 

?         Filter配置：

 

2        FilterToBeanProxy

 

&lt;filter&gt;

 

        &lt;filter-name&gt; springFilter &lt;/filter-name&gt;

 

        &lt;filter-class&gt;

 

            org.springframework.security.util.FilterToBeanProxy

 

        &lt;/filter-class&gt;

 

        &lt;init-param&gt;

 

            &lt;param-name&gt;targetBean&lt;/param-name&gt;

 

            &lt;param-value&gt;springFilter&lt;/param-value&gt;

 

        &lt;/init-param&gt;

 

    &lt;/filter&gt;

 

说明：需要为FilterToBeanProxy提供上下文参数，这里我们配置的是targetBean属性，它告诉spring在context中查找的bean名称，所以当请求被过滤器拦截后FilterToBeanProxy会在applicationContext.xml中会查找id为springFilter的bean.

 

我们也可以配置targetClass属性，意思就是查找该类型的bean.

 

2        DelegatingFilterProxy

 

&lt;filter&gt;

 

        &lt;filter-name&gt;springFilter&lt;/filter-name&gt;

 

        &lt;filter-class&gt;

 

            org.springframework.web.filter.DelegatingFilterProxy

 

        &lt;/filter-class&gt;

 

    &lt;/filter&gt;

 

 

 

说明：使用DelegatingFilterProxy时不需要配置任何参数，spring会根据filter-name的名字来查找bean，所以这里spring会查找id为springFilter的bean.

 

 

 

4\.       配置filter的mapping

 

&lt;filter-mapping&gt;

 

        &lt;filter-name&gt;springFilter&lt;/filter-name&gt;

 

        &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;

 

    &lt;/filter-mapping&gt;

 

 

 

OK!filter配置完成。推荐使用DelegatingFilterProxy，应为配置上更简单。

 

Servlet

Servlet的配置与Filter的配置十分相似

 

1\.       在applicationContext.xml中定义servlet

 

    &lt;bean id="springServlet" class="com.netqin.servlet.SpringServlet"&gt;

 

              &lt;property name="name"&gt;

 

                  &lt;value&gt;SpringServlet&lt;/value&gt;

 

              &lt;/property&gt;

 

       &lt;/bean&gt;

 

说明：com.netqin.servlet.SpringServlet继承自

 

javax.servlet.http.HttpServlet

 

2\.       实现servlet代理

 

与filter不同，spring没有为servlet提供代理实现，需要我们自己来创建，不过放心，创建一个servlet代理十分简单，一个具体的实现如下：

 

import java.io.IOException;

 

import javax.servlet.GenericServlet;

 

import javax.servlet.Servlet;

 

import javax.servlet.ServletException;

 

import javax.servlet.ServletRequest;

 

import javax.servlet.ServletResponse;

 

import org.springframework.web.context.WebApplicationContext;

 

import org.springframework.web.context.support.WebApplicationContextUtils;

 

 

 

public class ServletToBeanProxy extends GenericServlet {

 

    private String targetBean;

 

    private Servlet proxy;

 

    public void init() throws ServletException {

 

        this.targetBean = getInitParameter("targetBean");

 

        getServletBean();

 

        proxy.init(getServletConfig());

 

    }

 

    public void service(ServletRequest req, ServletResponse res)

 

            throws ServletException, IOException {

 

        proxy.service(req, res);

 

    }

 

    private void getServletBean() {

 

        WebApplicationContext wac = WebApplicationContextUtils

 

                .getRequiredWebApplicationContext(getServletContext());

 

        this.proxy = (Servlet) wac.getBean(targetBean);

 

    }

 

}

 

说明：相信看了代码就明白了，它利用targetBean属性在spring中查找相应的servlet，

 

这很像FilterToBeanProxy的方式，所以我为其取名为ServletToBeanProxy。当然，我们也可以使用类似于DelegatingFilterProxy的方式，只需要将上述代码中标记为黄色的部分修改为this.targetBean =this.getServletName();即可，我们相应的命名为DelegatingServletProxy。

 

3\.       配置web.xml

 

?         初始化spring的context

 

与filter中的说明一致，不再赘述。

 

?         Servlet配置：

 

2        ServletToBeanProxy

 

&lt;servlet&gt;

 

        &lt;servlet-name&gt;springServlet&lt;/servlet-name&gt;

 

        &lt;servlet-class&gt;

 

            com.netqin.servlet.proxy.ServletToBeanProxy

 

        &lt;/servlet-class&gt;

 

        &lt;init-param&gt;

 

            &lt;param-name&gt;targetBean&lt;/param-name&gt;

 

            &lt;param-value&gt;springServlet&lt;/param-value&gt;

 

        &lt;/init-param&gt;

 

        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;

 

    &lt;/servlet&gt;

 

2        DelegatingServletProxy

 

&lt;servlet&gt;

 

        &lt;servlet-name&gt;springServlet&lt;/servlet-name&gt;

 

        &lt;servlet-class&gt;

 

            com.netqin.servlet.proxy.DelegatingServletProxy

 

        &lt;/servlet-class&gt;

 

        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;

 

    &lt;/servlet&gt;

 

4\.       配置servlet的mapping

 

&lt;filter-mapping&gt;

 

        &lt;filter-name&gt;springServlet&lt;/filter-name&gt;

 

        &lt;url-pattern&gt;/servlet/\*&lt;/url-pattern&gt;

 

    &lt;/filter-mapping&gt;

 

OK!servlet的配置完成。推荐使用DelegatingServletProxy，应为配置上更简单。