Spring MVC過濾器-委派過濾器代理(DelegatingFilterProxy)

    org.springframework.web.filter中有一個特殊的類——DelegatingFilterProxy,該類其實並不能說是一個過濾器,它的原型是FilterToBeanProxy,即將Filter做爲spring的bean,由spring來管理。java

    配置DelegatingFilterProxy的經常使用方法以下所示:web

<filter>
		<filter-name>testFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetBeanName</param-name>
			<param-value>testBean</param-value>
		</init-param>
	</filter>        
        <filter-mapping>
		<filter-name>testFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

含義是有一個過濾器,它指向一個bean,這個bean在spring中的名字爲testBean,testBean也必需實現javax.servlet.Filter。spring

        其餘能夠經過web.xml傳遞的參數以下:session

        (1) contextAttribute,使用委派Bean的範圍,其值必須從org.springframework.context.ApplicationContext.WebApplicationContext中取得,默認值是session;app

        (2) targetFilterLifecycle,是否調用Filter的init和destroy方法,默認爲false。url

        因此DelegationgFilterProxy的全項配置信息以下:code

        <filter>
		<filter-name>testFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetBeanName</param-name>
			<param-value>testBean</param-value>
		</init-param>
                <init-param>
			<param-name>contextAttribute</param-name>
			<param-value>session</param-value>
		</init-param>
                <init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>false</param-value>
		</init-param>
	</filter>        
        <filter-mapping>
		<filter-name>testFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
相關文章
相關標籤/搜索