`

Spring管理filter和servlet(转)

 
阅读更多
Spring管理filter和servlet
  在使用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

<bean id="springFilter" class="com.netqin.filter.SpringFilter">

              <property name="name">

                  <value>SpringFilter</value>

              </property>

       </bean>


说明: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,一般来说配置如下:

<context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>

            /WEB-INF/applicationContext.xml

        </param-value>

    </context-param>

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

Filter配置:
FilterToBeanProxy

<filter>

        <filter-name> springFilter </filter-name>

        <filter-class>

            org.springframework.security.util.FilterToBeanProxy

        </filter-class>

        <init-param>

            <param-name>targetBean</param-name>

            <param-value>springFilter</param-value>

        </init-param>

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

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

DelegatingFilterProxy

<filter>

        <filter-name>springFilter</filter-name>

        <filter-class>

            org.springframework.web.filter.DelegatingFilterProxy

        </filter-class>

    </filter>



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



4. 配置filter的mapping

<filter-mapping>

        <filter-name>springFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>



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

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

1.       在applicationContext.xml中定义servlet

    <bean id="springServlet" class="com.netqin.servlet.SpringServlet">

              <property name="name">

                  <value>SpringServlet</value>

              </property>

       </bean>


说明: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配置:
ServletToBeanProxy

<servlet>

        <servlet-name>springServlet</servlet-name>

        <servlet-class>

            com.netqin.servlet.proxy.ServletToBeanProxy

        </servlet-class>

        <init-param>

            <param-name>targetBean</param-name>

            <param-value>springServlet</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>


DelegatingServletProxy

<servlet>

        <servlet-name>springServlet</servlet-name>

        <servlet-class>

            com.netqin.servlet.proxy.DelegatingServletProxy

        </servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

4.       配置servlet的mapping

<filter-mapping>

        <filter-name>springServlet</filter-name>

        <url-pattern>/servlet/*</url-pattern>

    </filter-mapping>

OK!servlet的配置完成。推荐使用DelegatingServletProxy,应为配置上更简单。
分享到:
评论

相关推荐

    Spring 管理filter 和servlet

    通过Spring管理Filter和Servlet,不仅可以充分利用Spring的依赖注入能力,简化Filter和Servlet的配置,还能增强代码的可维护性和可扩展性。开发者无需在Filter或Servlet内部硬编码bean名称,而是通过Spring容器自动...

    web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序

    Spring框架是Java平台的一个广泛使用的框架,用于管理对象的生命周期和依赖注入。在`web.xml`中配置Spring时,通常通过Listener来加载Spring容器,这样可以在应用程序启动时初始化所有的Spring Bean。 **示例代码:...

    基于Spring和Mybatis和servlet的人员信息管理系统.zip

    【标题】基于Spring和Mybatis和servlet的人员信息管理系统 这个系统是计算机科学与技术领域的一个典型项目,主要用于教学目的,如毕业设计、课程设计或毕设项目。它涉及了Web开发中的多个核心技术,包括Spring框架...

    Spring Boot 中的Servlet简单使用

    在Spring Boot中,Servlet的使用变得非常简便,因为Spring Boot内置了Servlet容器,如Tomcat或Jetty,这些容器会自动处理Servlet的注册和管理。本文将详细介绍如何在Spring Boot中添加自定义Servlet。 首先,Spring...

    Servlet之Filter深入讲解及实例研究

    Spring提供了一种更方便的方式来注册和管理Filter,即通过`@WebFilter`注解或Spring的`FilterRegistrationBean`。 7. **实例研究** 在给定的链接`http://blog.csdn.net/evankaka/article/details/45480101`中,博...

    Servlet Filter 的单元测试

    进行Servlet Filter的单元测试是确保Filter功能正确性和健壮性的重要步骤。 首先,理解Servlet Filter的工作原理至关重要。Filter通过实现`javax.servlet.Filter`接口,并重写`doFilter`方法来实现对HTTP请求的拦截...

    Spring4 In Action-7.1.2-添加其他的Servlet和Filter

    Spring4 In Action-7.1.2-添加其他的Servlet和Filter,Spring4 In Action-7.1.2-添加其他的Servlet和Filter,Spring4 In Action-7.1.2-添加其他的Servlet和Filter

    spring boot 开发 servlet filters listeners

    尽管 Spring MVC 控制器可以处理大部分的 Web 请求,但在某些场景下,我们可能还需要使用到 Servlet、Filter 和 Listener 这些技术来增强应用的功能。 #### 二、Servlet、Filter 和 Listener 的作用 1. **Servlet*...

    servlet之Filter使用范例--登陆验证

    在Java Web开发中,Servlet和Filter是两个非常重要的概念。Servlet是Java提供的一个用于扩展服务器功能的接口,而Filter则是一种动态web资源过滤机制,它允许我们在数据处理流程中进行预处理和后处理,比如登录验证...

    特殊情况(ActionForm,Servlet, Filter, Listener)下Spring如何注入对象

    总之,面对ActionForm、Servlet、Filter、Listener这些特殊情况,我们需要灵活运用Spring的DI机制,结合静态字段、工厂方法以及Web容器的特性,以确保对象的正确注入和管理。同时,随着技术的发展,如Spring Boot和...

    spring MVC所需jar包和filter的配置

    在开发基于Spring MVC的Web应用程序时,正确配置所需的jar包和Filter是至关重要的步骤。Spring MVC是一个强大的MVC(Model-View-Controller)框架,它为构建Java Web应用提供了丰富的功能和灵活性。以下是对标题和...

    Spring Security如何在Servlet中执行

    Spring Security 是一个强大的安全框架,...综上所述,Spring Security通过集成到Servlet Filter Chain中,利用一系列的Filter实现了复杂的安全管理。理解这些核心概念对于有效地使用和定制Spring Security至关重要。

    基于Jsp+Tomcat+Filter+Servlet的学生管理系统.zip

    它实现了Java EE中的Web应用服务器规范,包括Servlet和JSP。在这个项目中,Tomcat作为运行环境,负责解析和执行JSP、Servlet,并提供HTTP服务。 **3. Filter(过滤器)** 在Java Web中,Filter是一种可以拦截请求和...

    使用filter拦截servlet和jsp页面的内容,进行过滤后输出

    本文将详细介绍如何使用filter来对servlet和jsp页面的内容进行过滤,并在过滤后输出。 首先,了解Servlet Filter的基本概念。Filter是Java Servlet API的一部分,它允许开发者在请求到达目标Servlet或JSP之前以及...

    spring-boot-04-servlet.rar

    在Spring Boot中,可以使用`@WebServlet`和`@WebFilter`注解来注册Servlet和Filter。此外,还可以通过`ServletRegistrationBean`和`FilterRegistrationBean`来配置。 5. **使用Spring MVC**: Spring Boot中的...

    Spring Boot使用Servlet及Filter过程详解

    在 Spring Boot 应用程序中,Servlet 和 Filter 是两个非常重要的概念,它们都是 Java Servlet 规范的一部分。在本文中,我们将详细介绍 Spring Boot 使用 Servlet 及 Filter 过程etailed解。 Servlet 的概念 ...

    Servlet--2.filter

    在Java Web开发中,Servlet和Filter是两个非常重要的概念,特别是在构建动态Web应用程序时。`Servlet` 是Java提供的一种用于扩展服务器功能的接口,而`Filter` 则是在Servlet之上的一种高级机制,它允许开发者对HTTP...

    spring-boot 过滤器 filter

    Filter的生命周期由Servlet容器管理,包括初始化、服务和销毁三个阶段。初始化时,容器会调用`init()`方法;每当有请求匹配到过滤器时,会调用`doFilter()`方法;当应用停止或服务器关闭时,会调用`destroy()`方法。...

    servlet用户管理系统

    - **EJB(Enterprise JavaBeans)** 或 **Spring框架**:在大型项目中,可能会使用这些框架来管理事务、提供依赖注入和更高级的持久化机制。 总的来说,"servlet用户管理系统"是一个典型的Web应用实例,涵盖了...

Global site tag (gtag.js) - Google Analytics