javascript
如何使用Spring管理Filter和Servlet
在使用spring容器的web應(yīng)用中,業(yè)務(wù)對象間的依賴關(guān)系都可以用context.xml文件來配置,并且由spring容器來負責(zé)依賴對象 的創(chuàng)建。如果要在filter或者servlet中使用spring容器管理業(yè)務(wù)對象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())來獲得WebApplicationContext,然后調(diào)用WebApplicationContext.getBean(“beanName”)來獲得對象的引用,這實際上是使用了依賴查找來獲得對象,并且在filter或者servlet代碼中硬編碼了應(yīng)用對象的bean名字。為了能在filter或者servlet中感知spring中bean,可采用如下步驟來實現(xiàn):
1、將filter或者servlet作為bean定義在context.xml文件中,和要應(yīng)用的bean定義放在一起;2、實現(xiàn)一個filter代理或者servlet代理,該代理用WebApplicationContext來獲得在context.xml中定義的filter或者servlet的對象,并將任務(wù)委托給context.xml中定義的filter或者servlet3、在web.xml中用ContextLoaderListener來初始化spring 的context,同時在filter代理或者servlet代理的定義中用初始化參數(shù)來定義context.xml中filter或者servlet的bean名字(或者直接受用代理的名稱獲得相應(yīng)的filter或者servlet的名稱)。4、在web.xml中定義filter代理或者servlet代理的mapping。利用這種方式就將filter或者servlet和業(yè)務(wù)對象的依賴關(guān)系用spring 來進行管理,并且不用在servlet中硬編碼要引用的對象名字。具體實例如下:1、spring與web配置
1.1 在applicationContext.xml中定義filter
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>
2、Filter配置:
2.1 FilterToBeanProxy
<filter>
2.2 DelegatingFilterProxy
<filter>
</filter>
說明:使用DelegatingFilterProxy時不需要配置任何參數(shù),spring會根據(jù)filter-name的名字來查找bean,所以這里spring會查找id為springFilter的bean.
2.3 配置filter的mapping
<filter-mapping>
</filter-mapping>
filter配置完成。推薦使用DelegatingFilterProxy,應(yīng)為配置上更簡單。
3、Servlet 配置
Servlet的配置與Filter的配置十分相似
3.1 在applicationContext.xml中定義servlet
說明:com.sirui.servlet.SpringServlet繼承自 javax.servlet.http.HttpServlet
3.2 實現(xiàn)servlet代理,與filter不同,spring沒有為servlet提供代理實現(xiàn),需要我們自己來創(chuàng)建,不過放心,創(chuàng)建一個servlet代理十分簡單,一個具體的實現(xiàn)如下:
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)
}
說明:相信看了代碼就明白了,它利用targetBean屬性在spring中查找相應(yīng)的servlet,這很像FilterToBeanProxy的方式,所以我為其取名為ServletToBeanProxy。當然,我們也可以使用類似于DelegatingFilterProxy的方式,只需要將上述代碼中標記為黃色的部分修改為this.targetBean=this.getServletName();即可,我們相應(yīng)的命名為DelegatingServletProxy。
3.3 ServletToBeanProxy
<servlet>
</servlet>
3.4DelegatingServletProxy
<servlet>
3.5 配置servlet的mapping
<filter-mapping>
servlet的配置完成。推薦使用DelegatingServletProxy,應(yīng)為配置上更簡單。
更多技術(shù)分享盡在java樂園
總結(jié)
以上是生活随笔為你收集整理的如何使用Spring管理Filter和Servlet的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一次用51的博客,记录一下笔记
- 下一篇: Android热修复升级探索——SO库修