当我们用Filter时,往往需要使用一些辅助的service,在普通的java中,只要声明(set,get方法)后在spring-application配置文件中配置就可以了,但是由于Filter与Listener需要配置在web.xml文件中,所以它们的对象是由容器创建的。通常在Spring的application-context.xml配置文件中编写的bean由Spring负责创建,所以直接在Spring配置文件配置过滤器与监听器是无法达到注入目的的。这时就需要配置一个spring提供的代理类。web.xml中配置Filter如下:

  <filter>
<filter-name>authFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>authFilterSeriver</param-value>
</init-param>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>

其中配置的authFilterSeriver是配置spring文件中bean的id,如下图:

<bean id="authFilterSeriver"  class="com.test.AuthFilter" ></bean>

 这样, AuthFilter中就可以调用spring管理的service了。

同理,可以自己写一个代理类,类似spring的类DelegatingFilterProxy。

package com.test.filter;  

import java.io.IOException;  

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
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; //Filter 的代理类。系统所有的 Filter 共用此一个
public class DelegatingFilterProxy implements Filter {
private String targetFilterBean;
private Filter proxy; @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
proxy.doFilter(servletRequest, servletResponse, filterChain);
} @Override
public void init(FilterConfig config) throws ServletException {
this.targetFilterBean = config.getInitParameter("targetFilterBean");
ServletContext servletContext = null;
servletContext = config.getServletContext();
WebApplicationContext wac = null;
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.proxy = (Filter) wac.getBean(this.targetFilterBean);
this.proxy.init(config);
} @Override
public void destroy() {}
}

  

使用 Spring 容器管理 Filter的更多相关文章

  1. 如何在自定义Listener(监听器)中使用Spring容器管理的bean

    正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...

  2. 为什么不在spring容器管理controller

    Spring容器与SpringMVC容器 1.疑问:为什么不用spring去管理所有类? 我们配置springMVC 中,为什么controller不直接交给spring 管理而要spring MVC ...

  3. Spring容器管理各种文件

    1. 导入文件 <import resource="applicationContext-dataSource.xml" /> 2. 引用资源配置文件 <cont ...

  4. 对Spring 容器管理事务支持的总结

    1.问题 Connection conn = DataSourceUtils.getConnection(); //开启事务 conn.setAutoCommit(false); try { Obje ...

  5. Spring加载applicationContext.xml实现spring容器管理的单例模式

    package com.etc.pojo; import org.springframework.context.ApplicationContext; import org.springframew ...

  6. Spring加载applicationContext.xml实现spring容器管理的几种方式

    package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...

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

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

  8. Spring 管理Filter和Servlet

    本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...

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

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

随机推荐

  1. android windowSoftInputMode属性详解(转)

    activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性. 这个属性能影响两件事情: [一]当有焦点产生时,软键盘是隐藏还是显示 [二]是否减少活动 ...

  2. HDU4004

    题目大意,有一条长度为L和河流,中间穿插n个石凳,青蛙跳m次经过石凳后到达对岸,求青蛙每次跳跃的最大距离的最小值 本题数据量大n<500000,显然简单的o(n*n)算法是通过不了的,在输入大量 ...

  3. 在C#中使用C++编写的类——用托管C++进行封装[转]

    现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况 下,有很多开发人员就面临了如何在C#中使用C++开发好的 ...

  4. Linux学习之十-Linux系统时间

    Linux系统时间 1.date命令用于查看以及修改Linux系统的时间,关于date命令的详细帮助文档如下 [root@localhost ~]# date --help Usage: date [ ...

  5. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...

  6. Java reference的种类及使用场景

    Java 中一共有 4 种类型的引用 : StrongReference. SoftReference. WeakReference 以及 PhantomReference (传说中的幽灵引用).这  ...

  7. django orm高级查询 F表达式和Q表达式以及分组annotate

    1.关联关系映射及查询1.1django默认开启延迟加载所有多对1和1对1如果不使用select_related(),需要会延迟加载获取到相关对象,因为延迟可能会造成n+1次查询的问题,所以便有了se ...

  8. vue 组件创建与销毁

    vue 组件(如对话框组件)实时创建与销毁: 使用v-if <search-history :show="showSearchHistory" @close="sh ...

  9. VMware厚置备延迟置零,厚置备置零,精简置备具体解释

    本文具体介绍VMware厚置备延迟置零,厚置备置零,精简置备的概念及选择使用 1.厚置备延迟置零(zeroed thick) 以默认的厚格式创建虚拟磁盘.创建过程中为虚拟磁盘分配所需空间.创建时不会擦 ...

  10. log4j入门及常用配置

      <pre class="java" name="code">import org.apache.log4j.BasicConfigurator; ...