`
琼海溜
  • 浏览: 18585 次
  • 性别: Icon_minigender_1
  • 来自: 海口
社区版块
存档分类
最新评论

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

1
0
分享到:
评论

相关推荐

    servlet与spring整合例子

    而在整合Spring后,我们可以使用Spring提供的`HttpServletBean`或者`FrameworkServlet`(例如`DispatcherServlet`)作为Servlet的基础类,这样Servlet实例就会自动由Spring容器管理。`DispatcherServlet`是Spring ...

    原生Servlet与Spring Controller性能比较--Controller项目

    1. **初始化性能**:Servlet需要在服务器启动时加载并初始化,而Spring MVC通过Spring容器管理Controller实例,一般使用懒加载,因此在应用启动时,Spring Controller的性能可能优于Servlet。 2. **请求处理速度**...

    Spring 管理filter 和servlet

    开发者无需在Filter或Servlet内部硬编码bean名称,而是通过Spring容器自动注入所需的依赖,大大提升了代码的整洁度和复用性。此外,Spring提供的Filter和Servlet代理类,进一步简化了集成过程,使开发者可以专注于...

    基于Servlet+Spring+Mybatis的客户关系管理系统.zip

    【标题】"基于Servlet+Spring+Mybatis的客户关系管理系统.zip"揭示了这是一个采用Java Web...深入研究这些代码可以帮助我们理解系统的具体实现方式,学习如何整合Servlet、Spring和Mybatis来构建一个完整的Web应用。

    struts 整合spring 例子,测试通过

    一旦Spring容器被启动,它会创建一个`WebApplicationContext`对象,并将其绑定到Servlet上下文(ServletContext)。应用程序中的组件可以通过`WebApplicationContextUtils`工具类获取到`ApplicationContext`实例: ...

    CXF整合Spring步骤

    整合 CXF 和 Spring 的主要好处在于,可以利用 Spring 的容器管理 CXF 组件,简化配置,同时利用 Spring 的事务管理、安全控制等特性,提升整个应用的可维护性和扩展性。 以下是 CXF 整合 Spring 的基本步骤: 1. ...

    java-spring整合

    Spring 框架的主要模块包括 Core Container、AOP、Web、Web-Servlet、Web-Struts 和 JDBC 等。 二、 Java-Spring 整合的优点 使用 Java-Spring 整合可以带来很多优点,例如: * 简化开发:Spring 框架提供了一个...

    SSH(Servlet+Spring+Hibernate)整合所需jar包

    Spring在SSH中作为核心容器,管理其他组件的生命周期和依赖关系,关键的jar包包括: - `spring-beans.jar`:包含Bean工厂和依赖注入功能。 - `spring-context.jar`:提供上下文支持,包括AOP、事件、国际化等。 - `...

    Spring3+springmvc+mybatis三大整合

    首先,Spring框架是整个系统的基础,它提供了一个容器来管理对象的生命周期和依赖关系。通过XML配置文件或注解方式,可以实现对象的自动装配。Spring的AOP功能允许我们定义切面,实现如日志记录、事务管理等功能,使...

    spring springmvc mybatis框架整合需要的jar包

    最后,应用的启动入口通常是一个Servlet或Filter,例如Spring的ContextLoaderListener或DelegatingFilterProxy,它们负责初始化Spring容器,并在Web应用启动时加载配置。 总结起来,整合Spring、SpringMVC和MyBatis...

    整合SSM(Spring+Spring+Mybatis)所有到的jar包和笔记

    这涉及到`spring-mvc` jar包,同时还需要`servlet-api.jar`和`jsp-api.jar`来支持Servlet和JSP运行。 3. **Mybatis配置**:Mybatis的jar包包括`mybatis.jar`和`mybatis-spring.jar`,前者包含Mybatis的主要功能,后...

    SSH整合源码(Struts+Spring+Hibernate整合用户注册例子)

    Struts和Spring之间通过ActionServlet和Spring的DispatcherServlet协同工作,而Spring与Hibernate之间的整合通常通过SessionFactoryBean和HibernateTemplate完成。 8. **用户注册流程**:用户提交注册信息后,...

    struts2_mybatis_spring_框架实例整合_数据库 文档

    Spring框架作为全能型的IoC(Inversion of Control)/DI(Dependency Injection)容器,负责管理和装配应用中的各种组件。Spring还提供了AOP(面向切面编程)支持,用于实现如日志、事务管理等跨切面关注点。在...

    spring + spring mvc +mybatis整合开发所需jar包

    例如,Spring MVC中的DispatcherServlet就是由Spring容器管理的。 2. Spring MVC: Spring MVC 是Spring框架的一部分,专门用于处理Web请求。它遵循模型-视图-控制器(MVC)设计模式,负责接收请求、处理业务逻辑、...

    Spring的web整合包

    它包括了WebApplicationContext,这是Web应用特有的上下文,提供了与Servlet容器交互的能力。另外,Spring4.3.7还支持国际化(i18n)、主题(theme)和静态资源处理等功能。 在Spring4.3.7的Web整合包中,还包括了...

    mybatis与spring整合全部jar包

    SSM(Spring、SpringMVC、MyBatis)是一个经典的Java web开发框架组合,它将Spring作为核心容器,SpringMVC作为控制层,MyBatis作为数据访问层,提供了高效且灵活的Java web应用程序开发解决方案。这个“mybatis与...

    ssh整合 struts hibernate spring

    ### SSH整合Struts Hibernate Spring详解 #### 一、概述 SSH框架是指由Struts、Hibernate和Spring三个开源框架组成的轻量级Java企业级应用开发框架。这三个框架在Java Web开发中各自扮演着重要的角色:Struts负责...

    Spring+SpringMVC+Mybatis框架项目整合

    这些配置用于初始化Spring容器并管理各个组件。 3. **配置SpringMVC**:创建SpringMVC的配置文件(如`servlet-context.xml`),配置DispatcherServlet、ViewResolver、HandlerMapping等,定义MVC的行为和视图解析...

    web项目整合spring

    这些bean是Spring管理的对象,它们的生命周期由Spring容器控制。例如,我们可以在这里声明数据库连接池、数据源、事务管理器等。 2. **配置Web Dispatcher Servlet**:在`web.xml`中,我们需要配置一个Spring的...

Global site tag (gtag.js) - Google Analytics