在使用spring容器的web应用中,业务对象间的依赖关系均可以用context.xml文件来配置,而且由spring容器来负责依赖对象 的建立。若是要在filter或者servlet中使用spring容器管理业务对象,一般须要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来得到WebApplicationContext,而后调用WebApplicationContext.getBean(“beanName”)来得到对象的引用,这其实是使用了依赖查找来得到对象,而且在filter或者servlet代码中硬编码了应用对象的bean名字。为了能在filter或者servlet中感知spring中bean,可采用以下步骤来实现:java
一、将filter或者servlet做为bean定义在context.xml文件中,和要应用的bean定义放在一块儿;web
二、实现一个filter代理或者servlet代理,该代理用WebApplicationContext来得到在context.xml中定义的filter或者servlet的对象,并将任务委托给context.xml中定义的filter或者servletspring
三、在web.xml中用ContextLoaderListener来初始化spring 的context,同时在filter代理或者servlet代理的定义中用初始化参数来定义context.xml中filter或者servlet的bean名字(或者直接受用代理的名称得到相应的filter或者servlet的名称)。app
四、在web.xml中定义filter代理或者servlet代理的mapping。ui
利用这种方式就将filter或者servlet和业务对象的依赖关系用spring 来进行管理,而且不用在servlet中硬编码要引用的对象名字。具体实例以下:this
一、spring与web配置编码
1.1 在applicationContext.xml中定义filterurl
<bean id="springFilter" class="com.sirui.filter.SpringFilter"> 代理
<property name="name"> xml
<value>SpringFilter</value>
</property>
</bean>
说明:com.sirui.filter.SpringFilter为实现了javax.servlet.Filter接口的filter
实现filter代理 实际上,filter代理不须要咱们本身来实现,Spring提供了两种现成的filter代理 org.springframework.security.util.FilterToBeanProxy, org.springframework.web.filter.DelegatingFilterProxy,二者只是在web.xml中的配置上略有不一样,下面就让咱们一块儿看看如何在web.xml中进行配置。
1.2. 配置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配置:
2.1 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.
2.2 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.
2.3 配置filter的mapping
<filter-mapping>
<filter-name>springFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
filter配置完成。推荐使用DelegatingFilterProxy,应为配置上更简单。
三、Servlet 配置
Servlet的配置与Filter的配置十分类似
3.1 在applicationContext.xml中定义servlet
<bean id="springServlet" class="com.sirui.servlet.SpringServlet">
<property name="name">
<value>SpringServlet</value>
</property>
</bean>
说明:com.sirui.servlet.SpringServlet继承自 javax.servlet.http.HttpServlet
3.2 实现servlet代理,与filter不一样,spring没有为servlet提供代理实现,须要咱们本身来建立,不过放心,建立一个servlet代理十分简单,一个具体的实现以下:
package com.sirui.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。
配置web.xml和初始化spring的context 与filter中的说明一致,再也不赘述。
3.3 ServletToBeanProxy
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
com.sirui.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>
3.4DelegatingServletProxy
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>
com.sirui.servlet.proxy.DelegatingServletProxy
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
3.5 配置servlet的mapping
<filter-mapping>
<filter-name>springServlet</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
servlet的配置完成。推荐使用DelegatingServletProxy,应为配置上更简单。
关注公众号获取更多技术