相当于非 Boot Spring 项目的 org.springframework.boot.context.embedded.FilterRegistrationBean?

Jos*_*air 5 spring spring-mvc servlet-filters spring-bean spring-session

我正在尝试按照本教程在 Spring 中实现外部会话处理。不过,我在添加正确的过滤器时遇到了一些麻烦。Spring Boot 似乎定义了正确的 bean/过滤器,但我的项目不是 Spring Boot,所以它找不到FilterRegistrationBean. 在 Spring 的非 Boot 版本中是否有某种等价于这个类?我也试过org.springframework.web.context.embedded.FilterRegistrationBean,但无法正确导入(看起来这个文档指的是 SNAPSHOT 版本,所以也许这个包从来都不是正确版本的一部分)。

Sot*_*lis 3

根据您的容器配置、web.xml 或ServletContainerInitializer,您可以注册一个DelegatingFilterProxy过滤器并使其按名称引用Filter您在 .xml 中声明的 bean ApplicationContext

  • @JosephBlair `DelegatingFilterProxy` 是等效的。它不是被声明为 bean,而是在 XML (web.xml) 中声明为 Servlet `<filter>` 或通过 `WebApplicationInitializer` 中的 `ServletContext` 声明。 (2认同)
  • @JosephBlair通常你会有2个上下文,[一个根和一个servlet上下文](http://stackoverflow.com/questions/25610490/difference- Between-root-and-web-applicationcontext-spring-mvc)。根上下文由“ContextLoaderListener”加载。Servlet 上下文由“DispatcherServlet”加载。您的“Filter” bean 应该在根上下文中声明。这就是“DelegatingFilterProxy”将寻找它的地方。 (2认同)