本文转载自:http://www.open-open.com/lib/view/open1417248512252.html

在使用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:

  1. <bean id="springFilter" class="com.netqin.filter.SpringFilter">
  2. <property name="name">
  3. <value>SpringFilter</value>
  4. </property>
  5. </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,一般来说配置如下:

  1. <context-param>
  2.  
  3. <param-name>contextConfigLocation</param-name>
  4.  
  5. <param-value>
  6.  
  7. /WEB-INF/applicationContext.xml
  8.  
  9. </param-value>
  10.  
  11. </context-param>
  12.  
  13. <listener>
  14.  
  15. <listener-class>
  16.  
  17. org.springframework.web.context.ContextLoaderListener
  18.  
  19. </listener-class>
  20.  
  21. </listener>

Ø         Filter配置:

1.       FilterToBeanProxy

  1. <filter>
  2.  
  3. <filter-name> springFilter </filter-name>
  4.  
  5. <filter-class>
  6.  
  7. org.springframework.security.util.FilterToBeanProxy
  8.  
  9. </filter-class>
  10.  
  11. <init-param>
  12.  
  13. <param-name>targetBean</param-name>
  14.  
  15. <param-value>springFilter</param-value>
  16.  
  17. </init-param>
  18.  
  19. </filter>

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

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

2.      DelegatingFilterProxy

  1. <filter>
  2.  
  3. <filter-name>springFilter</filter-name>
  4.  
  5. <filter-class>
  6.  
  7. org.springframework.web.filter.DelegatingFilterProxy
  8.  
  9. </filter-class>
  10.  
  11. </filter>

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

4.       配置filter的mapping

  1. <filter-mapping>
  2.  
  3. <filter-name>springFilter</filter-name>
  4.  
  5. <url-pattern>/*</url-pattern>
  6.  
  7. </filter-mapping>

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

Servlet

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

1.       在applicationContext.xml中定义servlet

  1. <bean id="springServlet" class="com.netqin.servlet.SpringServlet">
  2.  
  3. <property name="name">
  4.  
  5. <value>SpringServlet</value>
  6.  
  7. </property>
  8.  
  9. </bean>

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

javax.servlet.http.HttpServlet

2.       实现servlet代理

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

  1. import java.io.IOException;
  2.  
  3. import javax.servlet.GenericServlet;
  4.  
  5. import javax.servlet.Servlet;
  6.  
  7. import javax.servlet.ServletException;
  8.  
  9. import javax.servlet.ServletRequest;
  10.  
  11. import javax.servlet.ServletResponse;
  12.  
  13. import org.springframework.web.context.WebApplicationContext;
  14.  
  15. import org.springframework.web.context.support.WebApplicationContextUtils;
  16.  
  17. public class ServletToBeanProxy extends GenericServlet {
  18.  
  19. private String targetBean;
  20.  
  21. private Servlet proxy;
  22.  
  23. public void init() throws ServletException {
  24.  
  25. this.targetBean = getInitParameter("targetBean");
  26.  
  27. getServletBean();
  28.  
  29. proxy.init(getServletConfig());
  30.  
  31. }
  32.  
  33. public void service(ServletRequest req, ServletResponse res)
  34.  
  35. throws ServletException, IOException {
  36.  
  37. proxy.service(req, res);
  38.  
  39. }
  40.  
  41. private void getServletBean() {
  42.  
  43. WebApplicationContext wac = WebApplicationContextUtils
  44.  
  45. .getRequiredWebApplicationContext(getServletContext());
  46.  
  47. this.proxy = (Servlet) wac.getBean(targetBean);
  48.  
  49. }
  50.  
  51. }

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

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

3.       配置web.xml

Ø         初始化spring的context

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

Ø         Servlet配置:

1.       ServletToBeanProxy

  1. <servlet>
  2.  
  3. <servlet-name>springServlet</servlet-name>
  4.  
  5. <servlet-class>
  6.  
  7. com.netqin.servlet.proxy.ServletToBeanProxy
  8.  
  9. </servlet-class>
  10.  
  11. <init-param>
  12.  
  13. <param-name>targetBean</param-name>
  14.  
  15. <param-value>springServlet</param-value>
  16.  
  17. </init-param>
  18.  
  19. <load-on-startup>1</load-on-startup>
  20.  
  21. </servlet>

2.       DelegatingServletProxy

  1. <servlet>
  2.  
  3. <servlet-name>springServlet</servlet-name>
  4.  
  5. <servlet-class>
  6.  
  7. com.netqin.servlet.proxy.DelegatingServletProxy
  8.  
  9. </servlet-class>
  10.  
  11. <load-on-startup>1</load-on-startup>
  12.  
  13. </servlet>

4.       配置servlet的mapping

  1. <filter-mapping>
  2.  
  3. <filter-name>springServlet</filter-name>
  4.  
  5. <url-pattern>/servlet/*</url-pattern>
  6.  
  7. </filter-mapping>

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

Spring 管理Filter和Servlet的更多相关文章

  1. Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)

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

  2. 如何使用Spring管理Filter和Servlet

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

  3. 配置DelegatingFilterProxy使用Spring管理filter chain

    项目环境:JDK7 + Maven3.04 0. 项目使用springmvc作为controller层 1. 引入spring-security <dependency> <grou ...

  4. 将spring管理的bean使用注解的方式注入到servlet中

    Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下: 1.创建一个AbstractServlet 抽象类,让你的所 ...

  5. 使用 Spring 容器管理 Filter

    当我们用Filter时,往往需要使用一些辅助的service,在普通的java中,只要声明(set,get方法)后在spring-application配置文件中配置就可以了,但是由于Filter与L ...

  6. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  7. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

  8. 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

    在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...

  9. Spring Boot中使用Servlet与Filter

    在Spring Boot中使用Servlet,根据Servlet注册方式的不同,有两种使用方式.若使用的是Servlet3.0+版本,则两种方式均可使用:若使用的是Servlet2.5版本,则只能使用 ...

随机推荐

  1. Zookeeper java api

     Zookeeper java api 主要有以下几个: 方法名称 描述 String create(final String path, byte data[], List acl, CreateM ...

  2. HDU4910 Problem about GCD

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  3. 一切从Trade开始(转)

    taobao.trades.sold.get 取得交易列表 从店铺取得时间 taobao.shop.get (nick,field)shop.created 循环读取从开店时间到当前时间的所有交易 t ...

  4. 读懂 ECMAScript 规格

    概述 规格文件是计算机语言的官方标准,详细描述语法规则和实现方法. 一般来说,没有必要阅读规格,除非你要写编译器.因为规格写得非常抽象和精炼,又缺乏实例,不容易理解,而且对于解决实际的应用问题,帮助不 ...

  5. 解决maven项目Cannot change version of project facet Dynamic web module to 3.0/3.1

    解决maven项目Cannot change version of project facet Dynamic web module to 3.0 1.打开项目所在目录下的.settings文件夹 打 ...

  6. spring mvc:输出json,输出多个json

    spring mvc:输出xml/输出json 用到的注解@ResponseBody @ResponseBody用来输出json/xml等格式数据(非html) controller输出用到的类 or ...

  7. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  8. group_concat长度限制

    #在MySQL配置文件(my.ini)中默认无该配置项,使用默认值时,值为1024,可在客户端执行下列语句修改: #SET GLOBAL group_concat_max_len = 1024; #该 ...

  9. IOS-CocoaPods的详细安装与使用

    1.为什么需要CocoaPods 在进行iOS开发的时候,总免不了使用第三方的开源库,比如SBJson.AFNetworking.Reachability等等.使用这些库的时候通常需要: 下载开源库的 ...

  10. Inventory Update

    依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的 ...