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,應為配置上更簡單。