您的位置:首页 > 编程语言 > Java开发

使用 spring 容器管理 Filter

2016-05-12 14:08 495 查看
使用 spring 容器管理 Filter

        《使用
Spring 容器管理 Servlet》一文介绍了如何使用 Spring 对 Servlet 进行管理,本文是《使用
Spring 容器管理 Servlet》的姊妹篇,本文介绍如何使用 Spring 对 Filter 进行管理。其实具体原理方法和前者大同小异。

        如一般的 J2EE 配置一样,Spring 在 web.xml 中的配置:

[html] view
plain copy

 print?

<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 中定义我们的业务逻辑处理类:

[html] view
plain copy

 print?

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

[java] view
plain copy

 print?

private LogService logService;  

public LogService getLogService() {  

    return logService;  

}  

  

public void setLogService(LogService logService) {  

    this.logService = logService;  

}     

        在 Filter 中如同一般的 Struts1/2 的 action 一样调用 service:

[java] view
plain copy

 print?

String ip = logService.getIpAddr(request);  

        如同一般的 Filter 我们的这个 Filter 需要实现 javax.servlet.Filter 接口:

[java] view
plain copy

 print?

public class BlockedIpFilter implements Filter {  

        根据自己的业务需要去实现 init、doFilter 和 destroy 方法:

[java] view
plain copy

 print?

@Override  

public 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);  

    }  

  

}  

  

@Override  

public void init(FilterConfig arg0) throws ServletException {  

    // TODO Auto-generated method stub  

}  

  

@Override  

public void destroy() {  

    // TODO Auto-generated method stub  

}  

        新建一个 Filter 代理类,这个类类似于 DelegatingFilterProxy 那样的代理,通过代理根据配置来找到实际的 Filter,完成业务逻辑功能:

[java] view
plain copy

 print?

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;  

  

    @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() {}  

}  

        不同于一般的 Filter 在 web.xml 中的配置,需要配置的是 Filter 代理类,而非 Filter:

[html] view
plain copy

 print?

<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 容器管理:

[html] view
plain copy

 print?

<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 javawe