問題:在項目中想在filter中注入bean服務,使用了不少的方法都測了,都沒有解決,偶爾在一篇博客中看到一個解決方法,最終解決了這個問題java
解決方法:主要使用 DelegatingFilterProxy 代理類,代碼以下spa
@Bean public FilterRegistrationBean filterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new DelegatingFilterProxy("resourceAuthorityFilter")); registration.addInitParameter("targetFilterLifecycle", "true"); registration.addUrlPatterns("/*"); registration.setDispatcherTypes(DispatcherType.REQUEST); return registration; } @Bean(name = "resourceAuthorityFilter") public ResourceAuthorityFilter resourceAuthorityFilter() { return new ResourceAuthorityFilter(); }