Kaiwii的专栏

System.out.println(“我是金融IT菜鸟");

DelegatingFilterProxy-api

  译者注:

为什么用DelegatingFilterProxy?窃以为,这样子做可以将DelegatingFilterProxy所代理的filter可以作为spring的bean,受到

spring的管理。而且有一点,我觉得应该注意的是,因为web应用的启动顺序是:listener->filter->servlet的,而spring容器的

启动主要是通过以下两种方式实现的:启动ContextLoaderListener或者ContextLoaderServlet的,所以,要

DelegatingFilterProxy所代理的filter可以作为spring的bean的话,就必须使用

启动ContextLoaderListener的方式。

by kaiwii

/**
 * Proxy for a standard Servlet 2.3 Filter, delegating to a Spring-managed
 * bean that implements the Filter interface. Supports a "targetBeanName"
 * filter init-param in <code>web.xml</code>, specifying the name of the
 * target bean in the Spring application context.
 *翻译:

类DelegatingFilterProxy作为标准的Servlet 2.3 Filter的代理,但是Filter的实际工作是通过将任务

委派给spring管理的而且实现了Filter接口的bean来完成的。通过在web.xml文件的名为"targetBeanName"
的filter的init-param变量来指定到底Spring容器(Spring application context)中哪个bean来作为实际执行

filter任务的bean(target bean)。


 * <p><code>web.xml</code> will usually contain a DelegatingFilterProxy definition,
 * with the specified <code>filter-name</code> corresponding to a bean name in
 * Spring's root application context. All calls to the filter proxy will then
 * be delegated to that bean in the Spring context, which is required to implement
 * the standard Servlet 2.3 Filter interface.

翻译:

web.xml通常会包括DelegatingFilterProxy的定义,其中的filter-name项会对应spring容器中的一个bean的名字。

所有的调用请求都会首先发送到这个filter代理中去,然后再委派到spring容器中的这个bean,当然,这个bean必须要

实现标准的Servlet 2.3 Filter 接口。
 *
 * <p>This approach is particularly useful for Filter implementation with complex
 * setup needs, allowing to apply the full Spring bean definition machinery to
 * Filter instances. Alternatively, consider standard Filter setup in combination
 * with looking up service beans from the Spring root application context.
 *翻译:

如果需要将Spring的bean命名机制应用到Filter实例上来完成复杂得启动任务,使用类DelegatingFilterProxy

就是一个很好的选择。除此之外,当标准的FIlter启动的时候需要使用到Spring容器里面的service bean的时候,

使用类DelegatingFilterProxy也是一个很好的选择。

 

 * <p><b>NOTE:</b> The lifecycle methods defined by the Servlet Filter interface
 * will by default <i>not</i> be delegated to the target bean, relying on the
 * Spring application context to manage the lifecycle of that bean. Specifying
 * the "targetFilterLifecycle" filter init-param as "true" will enforce invocation
 * of the <code>Filter.init</code> and <code>Filter.destroy</code> lifecycle methods
 * on the target bean, letting the servlet container manage the filter lifecycle.
 *翻译:

注意:Servlet Filter 接口中定义的生命周期函数默认不会被target bean自己执行(指:不是由服务器容器来调用),而是由spring容器(Spring application context )

执行来管理这个bean的生命周期。如果将名为"targetFilterLifecycle"的filter init-param参数设定为"true"的时候,target bean的Filter.init和Filter.destroy的

生命周期函数的调用就可以被执行了,换句话说,服务器容器(servlet container )就可以管理filter的生命周期了。


 * <p>This class is inspired by Acegi Security's FilterToBeanProxy class,
 * written by Ben Alex.

翻译:

这个类原型是FilterToBeanProxy 类
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
* @author 译者:kaiwii

 * @since 1.2
 * @see #setTargetBeanName
 * @see #setTargetFilterLifecycle
 * @see javax.servlet.Filter#doFilter
 * @see javax.servlet.Filter#init
 * @see javax.servlet.Filter#destroy
 */
public class DelegatingFilterProxy extends GenericFilterBean {

 private String contextAttribute;

 private String targetBeanName;

 private boolean targetFilterLifecycle = false;

 private Filter delegate;

 private final Object delegateMonitor = new Object();


 /**
  * Set the name of the ServletContext attribute which should be used to retrieve the
  * {@link WebApplicationContext} from which to load the delegate{@link Filter} bean.
  */
 public void setContextAttribute(String contextAttribute) {
  this.contextAttribute = contextAttribute;
 }

 /**
  * Return the name of the ServletContext attribute which should be used to retrieve the
  * {@link WebApplicationContext} from which to load the delegate{@link Filter} bean.
  */
 public String getContextAttribute() {
  return this.contextAttribute;
 }

 /**
  * Set the name of the target bean in the Spring application context.
  * The target bean must implement the standard Servlet 2.3 Filter interface.
  * <p>By default, the <code>filter-name</code> as specified for the
  * DelegatingFilterProxy in <code>web.xml</code> will be used.
  */
 public void setTargetBeanName(String targetBeanName) {
  this.targetBeanName = targetBeanName;
 }

 /**
  * Return the name of the target bean in the Spring application context.
  */
 protected String getTargetBeanName() {
  return this.targetBeanName;
 }

 /**
  * Set whether to invoke the <code>Filter.init</code> and
  * <code>Filter.destroy</code> lifecycle methods on the target bean.
  * <p>Default is "false"; target beans usually rely on the Spring application
  * context for managing their lifecycle. Setting this flag to "true" means
  * that the servlet container will control the lifecycle of the target
  * Filter, with this proxy delegating the corresponding calls.
  */
 public void setTargetFilterLifecycle(boolean targetFilterLifecycle) {
  this.targetFilterLifecycle = targetFilterLifecycle;
 }

 /**
  * Return whether to invoke the <code>Filter.init</code> and
  * <code>Filter.destroy</code> lifecycle methods on the target bean.
  */
 protected boolean isTargetFilterLifecycle() {
  return this.targetFilterLifecycle;
 }


 protected void initFilterBean() throws ServletException {
  // If no target bean name specified, use filter name.
  if (this.targetBeanName == null) {
   this.targetBeanName = getFilterName();
  }

  // Fetch Spring root application context and initialize the delegate early,
  // if possible. If the root application context will be started after this
  // filter proxy, we'll have to resort to lazy initialization.
  synchronized (this.delegateMonitor) {
   WebApplicationContext wac = findWebApplicationContext();
   if (wac != null) {
    this.delegate = initDelegate(wac);
   }
  }
 }

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
   throws ServletException, IOException {

  // Lazily initialize the delegate if necessary.
  Filter delegateToUse = null;
  synchronized (this.delegateMonitor) {
   if (this.delegate == null) {
    WebApplicationContext wac = findWebApplicationContext();
    if (wac == null) {
     throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
    }
    this.delegate = initDelegate(wac);
   }
   delegateToUse = this.delegate;
  }

  // Let the delegate perform the actual doFilter operation.
  invokeDelegate(delegateToUse, request, response, filterChain);
 }

 public void destroy() {
  Filter delegateToUse = null;
  synchronized (this.delegateMonitor) {
   delegateToUse = this.delegate;
  }
  if (delegateToUse != null) {
   destroyDelegate(delegateToUse);
  }
 }


 /**
  * Retrieve a <code>WebApplicationContext</code> from the <code>ServletContext</code>
  * attribute with the {@link #setContextAttribute configured name}. The
  * <code>WebApplicationContext</code> must have already been loaded and stored in the
  * <code>ServletContext</code> before this filter gets initialized (or invoked).
  * <p>Subclasses may override this method to provide a different
  * <code>WebApplicationContext</code> retrieval strategy.
  * @return the WebApplicationContext for this proxy, or <code>null</code> if not found
  * @see #getContextAttribute()
  */
 protected WebApplicationContext findWebApplicationContext() {
  String attrName = getContextAttribute();
  if (attrName != null) {
   return WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
  }
  else {
   return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  }
 }

 /**
  * Initialize the Filter delegate, defined as bean the given Spring
  * application context.
  * <p>Default implementation fetches the bean from the application context
  * and calls the standard <code>Filter.init</code> method on it, passing
  * in the FilterConfig of this Filter proxy.
  * @param wac the root application context
  * @return the initialized delegate Filter
  * @throws ServletException if thrown by the Filter
  * @see #getTargetBeanName()
  * @see #isTargetFilterLifecycle()
  * @see #getFilterConfig()
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
  Filter delegate = (Filter) wac.getBean(getTargetBeanName(), Filter.class);
  if (isTargetFilterLifecycle()) {
   delegate.init(getFilterConfig());
  }
  return delegate;
 }

 /**
  * Actually invoke the delegate Filter with the given request and response.
  * @param delegate the delegate Filter
  * @param request the current HTTP request
  * @param response the current HTTP response
  * @param filterChain the current FilterChain
  * @throws ServletException if thrown by the Filter
  * @throws IOException if thrown by the Filter
  */
 protected void invokeDelegate(
   Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
   throws ServletException, IOException {

  delegate.doFilter(request, response, filterChain);
 }

 /**
  * Destroy the Filter delegate.
  * Default implementation simply calls <code>Filter.destroy</code> on it.
  * @param delegate the Filter delegate (never <code>null</code>)
  * @see #isTargetFilterLifecycle()
  * @see javax.servlet.Filter#destroy()
  */
 protected void destroyDelegate(Filter delegate) {
  if (isTargetFilterLifecycle()) {
   delegate.destroy();
  }
 }

}

阅读更多
个人分类: J2EE spring
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭