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
,应为配置上更简单