我試圖在Spring引導配置中使用FilterRegistrationBean添加2個單獨的過濾器。如何在spring boot webapplication中配置2個獨立的過濾器?
這是我的2個過濾器的實現。
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterResgistrationAccess registration = new FilterResgistrationAccess();
registration.setFilter(new FeatureAccessFilter());
registration.addUrlPatterns("/api/*");
registration.setName("featureAccessFilter");
registration.addInitParameter("paramName", "paramValue");
registration.setOrder(1);
return registration;
}
@Bean
public FilterRegistrationBean shareFilterRegistration() {
FilterResgistrationShare registration = new FilterResgistrationShare();
registration.setFilter(new ShareAccessFilter());
registration.addUrlPatterns("/content_external/*");
registration.setName("shareAccessFilter");
registration.setOrder(2);
return registration;
}
但它給出了以下錯誤並未能啓動應用程序上下文。 請讓我知道我該如何做到這一點。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'featureAccessFilter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.boot.context.embedded.FilterRegistrationBean com.beroe.insync2.config.FeatureAccessFilter.filterRegistrationBean; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.boot.context.embedded.FilterRegistrationBean] is defined: expected single matching bean but found 2: someFilterRegistration,shareFilterRegistration at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
看起來Spring在創建FeatureAccessFilter實例時失敗了。請張貼相同的代碼。正如@guchuan所建議的那樣,使用限定符來定義並注入相同類型的bean –