在SpringBoot中是不需要配置web.xml的,那么原来在web.xml中配置的Servlet、Filter、Listener现在怎么弄呢?
SpringBoot提供了三种Bean FilterRegistrationBean
、ServletRegistrationBean
、ServletListenerRegistrationBean
分别对应配置原生的Filter、Servlet、Listener。
@Bean public ServletRegistrationBean indexServletRegistration() { ServletRegistrationBean registration = new ServletRegistrationBean(new IndexServlet()); registration.addUrlMappings("/hello"); return registration; } @Bean public FilterRegistrationBean indexFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(new IndexFilter()); registration.addUrlPatterns("/"); return registration; } @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new IndexListener()); return servletListenerRegistrationBean; }
原文地址:https://www.cnblogs.com/xiangkejin/p/9059930.html
时间: 2024-11-10 16:38:40