使用 Spring 容器管理 Filter
来源:互联网 发布:java常见项目 知乎 编辑:程序博客网 时间:2024/04/30 22:57
使用 Spring 容器管理 Filter
如一般的 J2EE 配置一样,Spring 在 web.xml 中的配置:
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext*.xml</param-value></context-param>
如一般的 J2EE 配置一样,Spring 在 applicationContext-service.xml 中定义我们的业务逻辑处理类:
<bean id="logService"class="com.defonds.cds.system.log.impl.LogServiceImpl"parent="baseService" scope="singleton" init-method="init" destroy-method="destroy"></bean>
如同一般的 Struts1/2 的 action 一样在我们的 Filter 中注入 service:
private LogService logService;public LogService getLogService() {return logService;}public void setLogService(LogService logService) {this.logService = logService;}
在 Filter 中如同一般的 Struts1/2 的 action 一样调用 service:
String ip = logService.getIpAddr(request);
如同一般的 Filter 我们的这个 Filter 需要实现 javax.servlet.Filter 接口:
public class BlockedIpFilter implements Filter {
根据自己的业务需要去实现 init、doFilter 和 destroy 方法:
@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {if (ifIpBlocked) {HttpServletRequest request = (HttpServletRequest)servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse; String ip = logService.getIpAddr(request);if (logService.ifBlocked(ip)) {response.getWriter().write("error");response.getWriter().close();} else {filterChain.doFilter(request, response);}} else {filterChain.doFilter(servletRequest, servletResponse);}}@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}@Overridepublic void destroy() {// TODO Auto-generated method stub}
新建一个 Filter 代理类,这个类类似于 DelegatingFilterProxy 那样的代理,通过代理根据配置来找到实际的 Filter,完成业务逻辑功能:
package com.defonds.cds.system.log.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;@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {proxy.doFilter(servletRequest, servletResponse, filterChain);}@Overridepublic 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);}@Overridepublic void destroy() {}}
不同于一般的 Filter 在 web.xml 中的配置,需要配置的是 Filter 代理类,而非 Filter:
<filter><filter-name>proxyBlockedIpFilterBean</filter-name><filter-class>com.defonds.cds.system.log.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterBean</param-name><param-value>blockedIpFilterBean</param-value></init-param></filter><filter-mapping><filter-name>proxyBlockedIpFilterBean</filter-name><url-pattern>/*</url-pattern></filter-mapping>
最后在 applicationContext-service.xml 中将 Filter 及其业务对象的依赖关系配置到 Spring 容器管理:
<bean id="blockedIpFilterBean" class="com.defonds.cds.system.log.filter.BlockedIpFilter"><property name="logService"><ref bean="logService"/></property></bean>
注意这里的 blockedIpFilterBean 要和 web.xml 配置的 blockedIpFilterBean 对应,logService 要和 Filter 中 get/set 得到的 logService bean 对应。
- 使用 Spring 容器管理 Filter
- 使用 spring 容器管理 Filter
- 使用 Spring 容器管理 Filter
- Java 使用 Spring 容器管理 Filter
- 在Servlet Filter中使用Spring容器
- 在Servlet Filter中使用Spring容器
- 使用 Spring 容器管理 Servlet
- 使用Spring容器管理JavaBean
- 使用 spring 容器管理 Servlet
- 使用 Spring 容器管理 Servlet
- 【备忘】Filter由容器管理,想交给spring管理,需用代理 org.springframework.web.filter.DelegatingFilterProxy
- Java 使用 Spring 容器管理 Servlet
- 使用spring安全容器管理web程序
- 使用spring容器管理和配置netty
- 使用spring容器管理和配置netty4
- 使用spring容器管理和配置netty
- spring管理filter
- Spring管理filter
- [网摘]android adb shell 命令大全
- iis中的asp.net程序无法访问oracle,提示“未找到oracle客户端和网络组件 ……”
- ORACLE SQL性能优化系列 (十一)
- Spring Security
- 关于j2me中Font的用法
- 使用 Spring 容器管理 Filter
- JAVA 取得当前日期的前一天,后一天
- 煎熬的10日凌晨
- java操作文件示例代码
- 孕妇的睡姿及其影响
- 对地址的一些操作
- JMS
- 教你如何秒杀12306,JAVA程序抢票成功!分享~~~[附截图]
- 促进高效数据传输的TCP/IP选项