Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)
具体实例如下:
Filter
1. 在applicationContext.xml中定义filter
<bean id="springFilter" class="com.netqin.filter.SpringFilter">
<property name="name">
<value>SpringFilter</value>
</property>
</bean>
说明:com.netqin.filter.SpringFilter为实现了javax.servlet.Filter接口的filter
2. 实现filter代理
实际上,filter代理不需要我们自己来实现,Spring提供了两种现成的filter代理
org.springframework.security.util.FilterToBeanProxy,
org.springframework.web.filter.DelegatingFilterProxy,两者只是在web.xml中的配置上略有不同,下面就让我们一起看看如何在web.xml中进行配置。
3. 配置web.xml
初始化spring的context
因为是使用spring来管理,所以在使用filter前先要初始化spring的context,一般来说配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Filter配置: FilterToBeanProxy
<filter>
<filter-name> springFilter </filter-name>
<filter-class>
org.springframework.security.util.FilterToBeanProxy
</filter-class>
<init-param>
<param-name>targetBean</param-name>
<param-value>springFilter</param-value>
</init-param>
</filter>
说明:需要为FilterToBeanProxy提供上下文参数,这里我们配置的是targetBean属性,它告诉spring在context中查找的bean名称,所以当请求被过滤器拦截后FilterToBeanProxy会在applicationContext.xml中会查找id为springFilter的bean.
我们也可以配置targetClass属性,意思就是查找该类型的bean.
<filter>
<filter-name>springFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
说明:使用DelegatingFilterProxy时不需要配置任何参数,spring会根据filter-name的名字来查找bean,所以这里spring会查找id为springFilter的bean.
配置filter的mapping
<filter-mapping>
<filter-name>springFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
OK!filter配置完成。推荐使用DelegatingFilterProxy,应为配置上更简单。
Servlet
Servlet的配置与Filter的配置十分相似
1. 在applicationContext.xml中定义servlet
<bean id="springServlet" class="com.netqin.servlet.SpringServlet">
<property name="name">
<value>SpringServlet</value>
</property>
</bean>
说明:com.netqin.servlet.SpringServlet继承自
javax.servlet.http.HttpServlet
2. 实现servlet代理
与filter不同,spring没有为servlet提供代理实现,需要我们自己来创建,不过放心,创建一个servlet代理十分简单,一个具体的实现如下:
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ServletToBeanProxy extends GenericServlet {
private String targetBean;
private Servlet proxy;
public void init() throws ServletException {
this.targetBean = getInitParameter("targetBean");
getServletBean();
proxy.init(getServletConfig());
}
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
proxy.service(req, res);
}
private void getServletBean() {
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
this.proxy = (Servlet) wac.getBean(targetBean);
}
}
说明:相信看了代码就明白了,它利用targetBean属性在spring中查找相应的servlet,
这很像FilterToBeanProxy的方式,所以我为其取名为ServletToBeanProxy。当然,我们也可以使用类似于DelegatingFilterProxy的方式,只需要将上述代码中标记为黄色的部分修改为this.targetBean=this.getServletName();即可,我们相应的命名为DelegatingServletProxy。
3. 配置web.xml
初始化spring的context
与filter中的说明一致,不再赘述。
Servlet配置:
ServletToBeanProxy
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
com.netqin.servlet.proxy.ServletToBeanProxy
</servlet-class>
<init-param>
<param-name>targetBean</param-name>
<param-value>springServlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
com.netqin.servlet.proxy.DelegatingServletProxy
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
4. 配置servlet的mapping
<filter-mapping>
<filter-name>springServlet</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
OK!servlet的配置完成。推荐使用DelegatingServletProxy,应为配置上更简单。
Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)的更多相关文章
- SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)
上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...
- SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean
SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean 在SpringBoot中开发AspectJ时,使用CTW的方式来织入代码,由于采用这种形式,切面Bean ...
- 非spring组件servlet、filter、interceptor中注入spring bean
问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...
- Spring 管理Filter和Servlet
本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...
- 如何使用Spring管理Filter和Servlet
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在filter或者servlet中使用sprin ...
- 如何在静态方法或非Spring Bean中注入Spring Bean
在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所 ...
- Java(多)线程中注入Spring的Bean
问题说明 今天在web应用中用到了Java多线程的技术来并发处理一些业务,但在执行时一直会报NullPointerException的错误,问题定位了一下发现是线程中的Spring bean没有被注入 ...
- java多线程中注入Spring对象问题
web应用中java多线程并发处理业务时,容易抛出NullPointerException. 原因: 线程中的Spring Bean没有被注入.web容器在启动时,没有提前将线程中的bean注入,在线 ...
- main方法中注入Spring bean
在有些情况下需要使用main使用Spring bean,但是main方法启动并没有托管给Spring管理,会导致bean失败,报空指针异常. 可以使用 ClassPathXmlApplicationC ...
随机推荐
- 关于proplists:get_value/2 与lists:keyfind/3 的效率比较
关于proplists:get_value/2 与lists:keyfind/2 的效率 早有比较,已出结论,lists:keyfind/2 的效率要好很多,好些人都是直接用或者做过它们之间的比较测试 ...
- Notepad++集成Subversion SVN插件
点击Plugin –> Plugin Manager –> Show Plugin Manager 打开后,在“Available”页找到“Subversion”,然后点击“Install ...
- jQuery 插件开发(1)
JavaScript 是一门混乱的语言,好的特性和坏的特性混杂在一起.而不同浏览器对标准的解析不一致,使得这门语言更加混乱,在这种情况下遵循最佳实践有诸多好处,至少不会掉入坑里.所以就有了<Ja ...
- 使用OpenSessionInViewFilter的注意事项
假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterc ...
- 微信小程序配置详解
在之前已经通过微信公众平台的官方网站https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html,注册好小程序并且登录成功后(这里主 ...
- Javascript闭包机制(转)
原文地址:http://www.felixwoo.com/archives/247 参考:http://www.ruanyifeng.com/blog/2009/08/learning_javascr ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- /etc/init.d/nginx
#! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon ...
- 不使用 spring-boot-starter-parent 构建 spring boot 应用
创建 spring-boot 应用通用方法是配置 pom.xml,定义 为 spring-boot-start-parent.如下: <parent> <groupId>org ...
- 使用CL命令编译cpp文件
缘起,我的vs 2003无法新建工程,又不喜欢用vs 2013那样的重量级开发工具(就写两行代码,测试测试一些基本的语法规则或算法). 想来vs应该可以像GCC或G++那样直接用命令行编译Cpp文件, ...