本文转载自: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:

 <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配置:

1.       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.

2.      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配置:

1.       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>

2.       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的更多相关文章

  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. LA 7272 Promotions(dfs)

    https://vjudge.net/problem/UVALive-7272 题意: 公司要提拔人,现在有n个人,现在有m条有向边,A->B表示A的表现比B好,也就是如果B晋升了,那么A肯定会 ...

  2. pycharm社区版创建django项目(Windows 8.1)

    django是Python的一个开源web框架,在pycharm开发环境中,pycharm专业版在新建一个项目的时候有django选项,帮助创建一个django框架的项目.pycharm社区版需要自己 ...

  3. response.getWriter().write("中文");乱码问题

    起初遇到这个问题,网上几乎所有的建议都是: response.setHeader("Content-type", "text/html;charset=UTF-8&quo ...

  4. gitlab 去除代码保护

  5. PHP运算符-算术运算符、三元运算符、逻辑运算符

    运算符是用来对变量.常量或数据进行计算的符号,它对一个值或一组值执行一个指定的操作.PHP的运算符包括算术运算符.字符串运算符.赋值运算符.位运算符.逻辑运算符.比较运算符.递增或递减运算符.错误控制 ...

  6. UML类图概述、设计模式

    深入浅出UML类图(http://blog.csdn.net/lovelion/article/details/7843308) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相 ...

  7. 转载:10+ handy and reusable jQuery code snippets

    源地址:http://www.catswhocode.com/blog/10-handy-and-reusable-jquery-code-snippets Smooth scrolling to t ...

  8. 1.SpringMVC设计理念与DispatcherServlet

    SpringMVC作为Struts2之后异军突起的一个表现层框架,正越来越流行,相信javaee的开发者们就算没使用过SpringMVC,也应该对其略有耳闻.我试图通过对SpringMVC的设计思想和 ...

  9. 1-25-循环控制符break、continue和函数详解

    大纲: 1-for循环补充 1-1-for循环实战---类C格式应用 2-break.continue循环控制符 2-1实战:帮助理解break.continue作用 3-函数详解 3-1.脚本文件中 ...

  10. hdu 4679 Terrorist’s destroy 树的直径+dp

    题意:给你一棵树,每条边都有值W,然后问你去掉一条边,令val = w*max(两颗新树的直径),求val最小值~ 做法,先求树的直径,然后算出直径上每个点的最长枝条长度.这样对于每一条边,假如是枝条 ...