在Filter中使用Spring Bean Injection

首先要在Spring中注册自己的Filter

 

     <bean id="adminSecurityFilter"       
            class="xxx.xxx.webapp.filter.AdminSecurityFilter" autowire="byName">      
     </bean>
 

 

 

然后在web.xml中定义Filter,并设置为Spring Filter代理

 

    <filter>
    	<description>
    	Only authorized user can visit Admin function</description>
    	<filter-name>adminSecurityFilter</filter-name>
    	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter> 

 

注意filter-name与在Spring Context中注册的bean id一致

 

最后,就可以在Filter类中自由使用已经在Spring中注册的Bean了

 

AuthorizedUserService authorizedUserService;
 

 

你可能感兴趣的:(spring,bean,Web,xml)