整合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。
整合Servlet到Spring容器的更多相关文章
- spring boot整合servlet、filter、Listener等组件方式
创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...
- spring boot 2.x 系列 —— spring boot 整合 servlet 3.0
文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...
- spring 整合 servlet
目的:记录spring整合 servlet过程demo.(企业实际开发中可能很少用到),融会贯通. 前言:在学习spring 过程(核心 ioc,aop,插一句 学了spring 才对这个有深刻概念, ...
- Servlet自动注入Spring容器中的Bean解决方法
很多情况在进行Web开发的时候需要自己手写Servlet来完成某些功能,而servlet有需要注入Spring容器中的某些bean,这是每次都要手动获取比较麻烦,这里有一个解决方案,只需要写一个ser ...
- 如何在servlet的监听器中使用spring容器的bean
另外补充下:在web Server容器中,无论是Servlet,Filter,还是Listener都不是Spring容器管理的,因此我们都无法在这些类中直接使用Spring注解的方式来注入我们需要的对 ...
- Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...
- servlet容器,web容器,spring容器,springmvc容器的区别(转)
web容器中有servlet容器,spring项目部署后存在spring容器和springmvc容器.其中spring控制service层和dao层的bean对象.springmvc容器控制contr ...
- Web容器、Servlet容器、Spring容器、SpringMVC容器之间的关系
以下内容为个人理解,如有误还请留言指出,不胜感激! Web容器 web容器(web服务器)主要有:Apache.IIS.Tomcat.Jetty.JBoss.webLogic等,而Tomcat.Jet ...
- Spring Boot整合Servlet,Filter,Listener,访问静态资源
目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...
随机推荐
- windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes 解决方案
win7 MySql5.6.17提示:1045 access denied for user 'root'@'localhost' using password yes 从网上找到的解决方法,以此博客 ...
- jboss CLI 命令行接口学习(适用JBOSS EAP 6.2+)
一.确认CLI所使用的端口 以domain模式为例,查看domain controller(也就是master主机)上的host.xml <management-interfaces> & ...
- [MetaHook] GameUI hook
Hook GameUI function. #include <metahook.h> #include <IGameUI.h> IGameUI *g_pGameUI = ; ...
- ASP.NET的编译原理
http://www.cnblogs.com/mdy2001212/archive/2008/01/31/1060345.html
- AVL树插入操作实现
为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...
- 1008win7与虚拟机中的linux共享文件的(详细)方法
转自http://jingyan.baidu.com/article/ca00d56c74dde4e99eebcfd2.html 好东西一起分享 win7与虚拟机中的linux共享文件的(详细)方法 ...
- LINUX中简单的字符命令
1. ls 查看目录中的内容 -a 查看隐藏文件 -l 显示文件的详细信息 -d 显示目录属性 -h 人性化显示文件大小 -i 显示ID号 2. 目录操作 创建目录 mkdir [-p](递归) di ...
- 【BZOJ 3048】【USACO2013 Jan】Cow Lineup 滑块思想
昨天下午想了好久没想出来,果然是很弱,思考能力低下. 用的类似单调队列的思想,维护一个长度为$k+1$的滑块,每次统计下$ans$就可以了 #include<cstdio> #includ ...
- 编译php5.4的时候出现错误----configure: error: in `/usr/local/src/php540/php-5.4.0':
错误如下:checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep - ...
- jQ1.5源码注释以及解读RE
jQ作为javascript的库( ▼-▼ ), 尽善尽美, 代码优美, 值得学习. 这一周平常上班没啥事也看jQ1.5的代码, 今天周六差不多看完了(Sizzle部分还没看), 重新看了一下, ...