spring管理filter與Servlet

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.