我知道过滤器可以使用@Order()进行注释,但是如果过滤器都包含在不同的第三方库中,那么我在创建bean时可以订购它们吗?

@Bean(Ordered.HIGHEST_PRECEDENCE) // Illegal!!!, just an example
SomeFilter someFilter() {
   // this runs before someOtherFilter
   return new SomeFilter();
}

@Bean(Ordered.LOWEST_PRECEDENCE) // Illegal!!!, just an example
SomeOtherFilter someOtherFilter() {
   // this runs after someFilter
   return new SomeOtherFilter();
}

最佳答案

由于无法在过滤器上添加@Order批注,因此仍可以像这样使用FilterRegistrationBean

    @Bean
    public FilterRegistrationBean someFilter()
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        SomeFilter filter = new SomeFilter();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns("/bla/*");
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
    }

07-26 09:34