整合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。

转载于:https://www.cnblogs.com/JAYIT/p/6092932.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值