在Spring Boot中为Servlet配置添加过滤器

时间:2014-09-04 16:43:21

标签: spring spring-mvc spring-boot spring-batch-admin

我正在使用spring-boot-starter-web包构建基于spring-batch-admin的webapp。

@Configuration
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class, DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class })
@Import(MainConfiguration.class)
@EnableTransactionManagement
public class BatchAdmin extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(BatchAdmin.class, args);
  }
  // ...
}      

@Configuration
@ComponentScan("com.company.package*")
@Import({ ServletConfiguration.class, WebappConfiguration.class })
public class MainConfiguration {}

@Configuration
@ImportResource("classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml")
public class ServletConfiguration {}

@Configuration
@ImportResource({ "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml","classpath:persistence-context.xml" })
public class WebappConfiguration {}

如何使用java配置样式将此过滤器添加到servlet上下文?

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1 个答案:

答案 0 :(得分:4)

有时(但不经常)查询文档比询问SO社区更快。

根据官方spring-boot documentation,我必须将以下bean定义添加到BatchAdmin类。

@Bean
public Filter hiddenHttpMethodFilter() {
   HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
   return filter;
}