在使用spring容器的web應用中,業務對象間的依賴關係均可以用context.xml文件來配置,而且由spring容器來負責依賴對象 的建立。若是要在filter或者servlet中使用spring容器管理業務對象,一般須要使用 java
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中硬編碼要引用的對象名字。 web
1. 在applicationContext.xml中定義filter spring
<bean id="springFilter" class="com.netqin.filter.SpringFilter"> app
<property name="name"> ui
<value>SpringFilter</value> this
</property> 編碼
</bean> url
說明:com.netqin.filter.SpringFilter爲實現了javax.servlet.Filter接口的filter spa
2. 實現filter代理 .net
實際上,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的配置與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>
如上參考>>>>>>>>>>>>>>