zoukankan      html  css  js  c++  java
  • 整合Servlet到Spring容器

    有时在Spring(3.2.5)项目中,如果使用到Servlet,可能希望Servlet实例作为bean受Spring容器管理,这样也能自动注入其他需要的bean,查了下,发现只针对过滤器提供了代理类org.springframework.web.filter.DelegatingFilterProxy,并没有提供针对Servlet的代理类,于是模仿着写了下面的代理类:
      
    package org.springframework.web.servlet;
     
    import java.io.IOException;
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.util.Assert;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
     
     
    /**
     *  Servlet代理类,将Servlet托管于Spring容器,以便直接在Servlet内部自动注入其他bean<br>
     *  参考 {@code org.springframework.web.filter.DelegatingFilterProxy}<br>
     */
    @SuppressWarnings("serial")
    public class DelegatingServletProxy extends HttpServletBean {
     
     private String contextAttribute;
     private WebApplicationContext webApplicationContext;
     private String targetBeanName;
     private boolean targetServletLifecycle = true;
     private volatile Servlet delegate;
     private final Object delegateMonitor = new Object();
     
     public DelegatingServletProxy() {
     }
     
     public DelegatingServletProxy(Servlet delegate) {
      Assert.notNull(delegate, "delegate Servlet object must not be null");
      this.delegate = delegate;
     }
     
     public DelegatingServletProxy(String targetBeanName) {
      this(targetBeanName, null);
     }
     
     public DelegatingServletProxy(String targetBeanName, WebApplicationContext wac) {
      Assert.hasText(targetBeanName, "target Servlet bean name must not be null or empty");
      this.setTargetBeanName(targetBeanName);
      this.webApplicationContext = wac;
      if (wac != null) {
       this.setEnvironment(wac.getEnvironment());
      }
     }
     
     public void setContextAttribute(String contextAttribute) {
      this.contextAttribute = contextAttribute;
     }
     
     public String getContextAttribute() {
      return this.contextAttribute;
     }
     
     public void setTargetBeanName(String targetBeanName) {
      this.targetBeanName = targetBeanName;
     }
     
     protected String getTargetBeanName() {
      return this.targetBeanName;
     }
     
     public void setTargetServletLifecycle(boolean targetServletLifecycle) {
      this.targetServletLifecycle = targetServletLifecycle;
     }
     
     protected boolean isTargetServletLifecycle() {
      return this.targetServletLifecycle;
     }
     
     @Override
     protected void initServletBean() throws ServletException {
      synchronized (this.delegateMonitor) {
       if (this.delegate == null) {
        if (this.targetBeanName == null) {
         this.targetBeanName = this.getServletName();
        }
        WebApplicationContext wac = this.findWebApplicationContext();
        if (wac != null) {
         this.delegate = this.initDelegate(wac);
        }
       }
      }
     }
     
     @Override
     public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
      Servlet delegateToUse = this.delegate;
      if (delegateToUse == null) {
       synchronized (this.delegateMonitor) {
        if (this.delegate == null) {
         WebApplicationContext wac = this.findWebApplicationContext();
         if (wac == null) {
          throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
         }
         this.delegate = this.initDelegate(wac);
        }
        delegateToUse = this.delegate;
       }
      }
      this.invokeDelegate(delegateToUse, req, resp);
     }
     
     @Override
     public void destroy() {
      Servlet delegateToUse = this.delegate;
      if (delegateToUse != null) {
       this.destroyDelegate(delegateToUse);
      }
     }
     
     protected WebApplicationContext findWebApplicationContext() {
      if (this.webApplicationContext != null) {
       if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
        if (!((ConfigurableApplicationContext) this.webApplicationContext).isActive()) {
         ((ConfigurableApplicationContext) this.webApplicationContext).refresh();
        }
       }
       return this.webApplicationContext;
      }
      String attrName = this.getContextAttribute();
      if (attrName != null) {
       return WebApplicationContextUtils.getWebApplicationContext(super.getServletContext(), attrName);
      }
      else {
       return WebApplicationContextUtils.getWebApplicationContext(super.getServletContext());
      }
     }
     
     protected Servlet initDelegate(WebApplicationContext wac) throws ServletException {
      Servlet delegate = wac.getBean(this.getTargetBeanName(), Servlet.class);
      if (this.isTargetServletLifecycle()) {
       delegate.init(super.getServletConfig());
      }
      return delegate;
     }
     
     protected void invokeDelegate(Servlet delegate, ServletRequest req, ServletResponse resp) throws ServletException, IOException {
      delegate.service(req, resp);
     }
     
     protected void destroyDelegate(Servlet delegate) {
      if (this.isTargetServletLifecycle()) {
       delegate.destroy();
      }
     }
     
    }
     
    //======================================================//
    用法:
    1、比如存在test.TestServlet,配置到spring对应xml中:
     <bean id="testServlet" class="test.TestServlet" />
     
    2、在web.xml配置如下:
    <servlet>
      <servlet-name>testServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DelegatingServletProxy</servlet-class>
      <init-param>
       <param-name>targetBeanName</param-name>
       <param-value>testServlet</param-value>
      </init-param>
      <load-on-startup>10</load-on-startup>
     </servlet>
     
    然后在TestServlet中可自动注入需要的bean。

  • 相关阅读:
    Android Studio轻松上手指南(1)
    在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程
    opencv提取截获图像(总结摘来)
    10.正则表达式匹配
    遍历 Map 的方式
    09.回文数
    08.字符串转换位整数
    07.整数反转
    06. Z字型变换
    05. 求最长回文子串
  • 原文地址:https://www.cnblogs.com/JAYIT/p/6092932.html
Copyright © 2011-2022 走看看