在使用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中寫入程式碼要引用的對象名字。
具體執行個體如下:
1.在applicationContext.xml中定義filter
<bean id="loginFilter" class="com.ces.system.filter.LoginFilter"> <property name="userDao"><ref bean="usersDaoImpl" /></property> </bean>
2. 實現filter代理
實際上,filter代理不需要我們自己來實現,Spring提供了兩種現成的filter代理
org.springframework.security.util.FilterToBeanProxy
org.springframework.web.filter.DelegatingFilterProxy
兩者只是在web.xml中的配置上略有不同,下面就讓我們一起看看如何在web.xml中進行配置。
3.配置web.xml
3.1 初始化spring的context
<!-- 指定Spring的設定檔 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring/*.xml</param-value></context-param><!-- 指定以Listener方式啟動Spring容器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
3.2 Filter配置:
FilterToBeanProxy:
<filter> <filter-name> loginFilter </filter-name> <filter-class> org.springframework.security.util.FilterToBeanProxy </filter-class> <init-param> <param-name>targetBean</param-name> <param-value>loginFilter</param-value> </init-param> </filter>
說明:需要為FilterToBeanProxy提供上下文參數,這裡我們配置的是targetBean屬性,它告訴spring在context中尋找的bean名稱,所以當請求被過濾器攔截後FilterToBeanProxy會在applicationContext.xml中會尋找id為loginFilter的bean.我們也可以配置targetClass屬性,意思就是尋找該類型的bean.
DelegatingFilterProxy:
<filter> <filter-name>loginFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter>
說明:使用DelegatingFilterProxy時不需要配置任何參數,spring會根據filter-name的名字來尋找bean,所以這裡spring會尋找id為loginFilter的bean.
4. 配置filter的mapping
<filter-mapping><filter-name>loginFilter</filter-name><url-pattern>*.action</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
3.1 初始化spring的context
與filter中的說明一致,不再贅述。
3.2
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,應為配置上更簡單