Spring管理filter和servlet

12 篇文章 0 订阅

Spring

管理

filter

servlet 

在使用

spring

容器的

web

应用中,业务对象间的依赖关系都可以用

context.xml

文件来

配置,

并且由

spring

容器来负责依赖对象

 

的创建。

如果要在

filter

或者

servlet

中使用

spring

容器管理业务对象,通常需要使用。

 

WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())

WebApplicationContext

,然后调用

WebApplicationContext.getBean("beanName")

来获得对

象的引用,这实际上是使用了依赖查找来获得对象,并且在

filter

或者

servlet

代码中硬编码

了应用对象的

bean

名字。为了能在

filter

或者

servlet

中感知

spring

bean

,可采用如下步

骤来实现:

 

 

1.

 

filter

或者

servlet

作为

bean

定义在

context.xml

文件中,

和要应用的

bean

定义放在一

起;

 

2.

 

实现一个

filter

代理或者

servlet

代理,该代理用

WebApplicationContext

来获得在

context.xml

中定义的

filter

或者

servlet

的对象,

并将任务委托给

context.xml

中定义的

filter

或者

servlet

 

3.

 

web.xml

中用

ContextLoaderListener

来初始化

spring 

context

,同时在

filter

代理或

servlet

代理的定义中用初始化参数来定义

context.xml

filter

或者

servlet

bean

字(或者直接受用代理的名称获得相应的

filter

或者

servlet

的名称)

 

4.

 

web.xml

中定义

filter

代理或者

servlet

代理的

mapping

 

 

利用这种方式就将

filter

或者

servlet

和业务对象的依赖关系用

spring 

来进行管理,并且

不用在

servlet

中硬编码要引用的对象名字。

 

 

具体实例如下:

 

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

 

 

DelegatingFilterProxy 

 

<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

 

 

4. 

配置

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> 

 

DelegatingServletProxy 

<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

,应为配置上更简单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值