使用 Spring 容器管理 Filter
当我们用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的更多相关文章
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...
- 为什么不在spring容器管理controller
Spring容器与SpringMVC容器 1.疑问:为什么不用spring去管理所有类? 我们配置springMVC 中,为什么controller不直接交给spring 管理而要spring MVC ...
- Spring容器管理各种文件
1. 导入文件 <import resource="applicationContext-dataSource.xml" /> 2. 引用资源配置文件 <cont ...
- 对Spring 容器管理事务支持的总结
1.问题 Connection conn = DataSourceUtils.getConnection(); //开启事务 conn.setAutoCommit(false); try { Obje ...
- Spring加载applicationContext.xml实现spring容器管理的单例模式
package com.etc.pojo; import org.springframework.context.ApplicationContext; import org.springframew ...
- Spring加载applicationContext.xml实现spring容器管理的几种方式
package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...
- Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...
- Spring 管理Filter和Servlet
本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...
- 如何使用Spring管理Filter和Servlet
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在filter或者servlet中使用sprin ...
随机推荐
- Discussion about z pre-pass
Z pre-pass In the rendering Process, the first pass render to a depth buffer to get the front layer ...
- JVM技术部分总结
1.JVM内存模型 1.1 JVM内存模型图解 Java虚拟机在执行Java程序的过程中,会把它所管理的内存划分为若干个不同的数据区.这些区域有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程 ...
- 接口测试 rest-assured 使用指南
转自:https://testerhome.com/topics/7060 原文:https://github.com/rest-assured/rest-assured/wiki/Usage本文gi ...
- 利用Acunetix WVS进行批量网站漏洞评估
我们知道Acunetix WVS可以对网站进行安全性评估,那么怎么能批量扫描呢?游侠(www.youxia.org)在测试WVS 8 BETA2的时候发现WVS居然支持WEB管理,还是很方便的. 打开 ...
- shell脚本学习笔记 (正則表達式)
正則表達式一般有三个部分组成,他们各自是:字符类,数量限定符,位置限定符. 规定一些特殊语法表示字符类.数 量限定符和位置关系,然后用这些特殊语法和普通字符一起表示一个模式,这就是正則表達式(Regu ...
- OCP学习基本知识点总结
下面是我总结的OCP教程的知识点.以备參考之用. 1, What's Oracle Server? · It's a database management system that ...
- 在非主线程中更新UI
在非主线程中调用了showMessage方法,结果报错:Can't create handler inside thread that has not called Looper.prepare() ...
- javascript获取星期
入门: var week = new Date().getDaty(); var ary = new Array("日","一","二",& ...
- Exception from HRESULT: 0x80070057 (E_INVALIDARG)
Exception from HRESULT: 0x80070057 (E_INVALIDARG)异常. 解决方案:清除ASP.NET缓存目录中对应的应用程序目录. ASP.NET缓存目录如下: C: ...
- C# MVC VS WebAPI
获取路径: MVC:Server.MapPath("/Templates/vshop/default.json") WebAPI:System.Web.Hosting.Hostin ...