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,应为配置上更简单。
最新文章
- 输出 Office 报表
- 初学python第一天
- php示例代码之 使用PHP的MySQL标准函数
- 80端口未占用,apache无法启动解决办法
- ios开发@selector的函数如何传参数/如何传递多个参数
- ubuntu下修改时区
- HTML5&;CSS3&;JavaScript&;PHP&;MySQL学习笔记
- 在Win32程序中显示Dos调试窗口,可暂停(AllocConsole,WriteConsole,FreeConsole函数,GetStdHandle函数取得输入句柄)
- ASP.NET MVC的Action Filter
- C++11 多线程 教学(2)
- numpy 札记
- Build MySQL 5.7.4 in RedHat
- H5缓存
- android判断网络是否可用
- parsing:NLP之chart parser句法分析器
- 阅读阿里文档总结————Mysql数据库篇
- Hadoop双namenode配置搭建(HA)
- Codeforces.472F.Design Tutorial: Change the Goal(构造 线性基 高斯消元)
- SpringBoot--web版的ocr
- FFT与一些冷门问题