一,在web.xml中配置DelegatingFilterProxy
<!-- 代理过滤器,用于在filter中使用service。service需在applicationContext.xml中单独配置 -->
<filter>
<filter-name>DelegatingFilterProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>YourFilter</param-value>
</init-param>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DelegatingFilterProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
二、applicationContext.xml中配置filter,将其纳入spring管理
<bean id="YourFilter" class="*.filter.YourFilter" ></bean>
三、在filter中注入使用即可
public class CheckLegalityFilterV2 extends OncePerRequestFilter{
@Autowired
privateMyService myService;
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
业务逻辑处理
}
}