使用 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 ...
随机推荐
- [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行
Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...
- ios notification
apps can use local or push notifications to let people know when interesting things happen, such as: ...
- cocos2dx 2.x新建项目
举例: cocos2d-x-2.2.6/tools/project-creator 进入 这个文件夹 chmod 777 project-cereator.py ./create_project.py ...
- angular js 使用$location问题整理
angular js 自带的$location方法十分强大,通过使用$location方法.我们能够获取到server的port.杂乱连接中的path()部分(/所包括的部分). 例: // give ...
- 2016.11.14 MIT challenge之课程总览
Degree Chartshttp://catalog.mit.edu/degree-charts/computer-science-engineering-course-6-3/ MIT Chall ...
- dockerfile VOLUME 对外暴露目录设置问题
最近遇到一个特殊问题, 容器对外映射挂载目录 , 发现容器中的目录并没有映射到宿主机上. 后经排查是是镜像中没有指定 volume # 例如 FROM debian:wheezy VOLUME /da ...
- hdu5386(暴力)
题意: 给出两个n*n的矩阵,一个作为初始矩阵.一个作为目标矩阵,给出m个操作,操作有两种,一种是"L,x,y".代表我们要把x这一行赋成y,还有一种是"H,x,y&qu ...
- Chrome自带恐龙小游戏的源码研究(五)
在上一篇<Chrome自带恐龙小游戏的源码研究(四)>中实现了障碍物的绘制及移动,从这一篇开始主要研究恐龙的绘制及一系列键盘动作的实现. 会眨眼睛的恐龙 在游戏开始前的待机界面,如果仔细观 ...
- 详解Linux三剑客之awk
第一篇 awk简介与表达式实例 一种名字怪异的语言 模式扫描和处理,处理数据和生成报告. awk不仅仅是linux系统中的一个命令,而且是一种编程语言:它可以用来处理数据和生成报告(excel):处理 ...
- apt-mirror 校验错误文件处理
apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...