springboot中@webfilter注解的filter时注入bean都是null
在使用@Webfilter注解Filter的情况下,不上外部tomcat时是没有问题的。但是在tomcat下运行时,filter中注入的bean就都是null
解决办法:
一:去掉@Webfilter注解
public class GeneralFilter implements Filter { @Autowired private SettingRemote settingRemote; //注入 .... }
二: 新增一个配置类,用来手工创建filter的bean
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Bean public Filter generalFilter() { return new GeneralFilter(); } @Bean public FilterRegistrationBean uploadFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new DelegatingFilterProxy("generalFilter")); registration.addUrlPatterns("/*"); registration.setName("generalFilter"); registration.setOrder(1); return registration; } }
之后注入的业务bean就不会是null