整理一下,关于Servlet Listener 和Filter 有两种注册方式:
1.基于注解
2.Java Config的形式
PS:本人的Spring版本是5.x 非4.x
先说第一种形式:SpringBoot: 注意要在启动类上加上@ServletComponentScan注解 不然Servlet,Listener,Filter都不会启作用
Servlet:
- @WebServlet(name="servletTest1",urlPatterns="/servlet/test")
- public class ServletTest1 extends HttpServlet
- {
- private static final long serialVersionUID = -5310239197651527329L;
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
- {
- System.out.println("this is my servlet get way");
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
- {
- System.out.println("this is my servlet post way");
- }
- }
Listener:
- @WebListener
- public class ListenerTest1 implements ServletContextListener
- {
- @Override
- public void contextDestroyed(ServletContextEvent arg0)
- {
- System.out.println("dddddd");
- }
- @Override
- public void contextInitialized(ServletContextEvent arg0)
- {
- System.out.println("this is listener,configured by annotation");
- }
- }
Filter:
- @WebFilter(urlPatterns="/annotation/test1")
- public class AnnotationFilterTest1 implements Filter
- {
- @Override
- public void init(FilterConfig filterConfig) throws ServletException
- {
- }
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException
- {
- System.out.println("this is filter by annotation");
- chain.doFilter(request, response);
- }
- @Override
- public void destroy()
- {
- }
- }
Interceptor:
SpringBoot不提供基于注解的形式注册Interceptor,所以只能手动注册啦
- public class MyInterceptorTest1 implements HandlerInterceptor
- {
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
- {
- System.out.println("在controller之前执行,只有这个返回true,postHandler才会执行");
- return HandlerInterceptor.super.preHandle(request, response, handler);
- }
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
- ModelAndView modelAndView) throws Exception
- {
- System.out.println("controller之后,在渲染页面之前执行");
- }
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
- throws Exception
- {
- System.out.println("渲染页面之后执行");
- }
- }
- @Configuration
- public class InterceptroConfiguration implements WebMvcConfigurer
- {
- @Override
- public void addInterceptors(InterceptorRegistry registry)
- {
- registry.addInterceptor(new MyInterceptorTest1()).addPathPatterns("/interceptor/*");
- }
- }
我的版本是Spring5.x WebMvcConfigurer 提供了默认的接口的实现方式,所以只需要implements即可,5.x以下的版本需要extends WebMvcConfigurerAdapter 即可
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------第二种形式:基于Java Config
Servlet: Servlet类的写法:只需要继承HttpServlet即可
- @Bean
- public ServletRegistrationBean<HttpServlet> servletRegistrationBean()
- {
- ServletRegistrationBean<HttpServlet> servletRegistrationBean = new ServletRegistrationBean<>();
- servletRegistrationBean.setServlet(new ServletTest1());
- servletRegistrationBean.addUrlMappings("/servlet/test2");
- return servletRegistrationBean;
- }
- @Bean
- public ServletRegistrationBean<HttpServlet> servletRegistrationBean3()
- {
- ServletRegistrationBean<HttpServlet> servletRegistrationBean = new ServletRegistrationBean<>();
- servletRegistrationBean.setServlet(new ServletJavaConfigTest3());
- servletRegistrationBean.addUrlMappings("/servlet/test3");
- return servletRegistrationBean;
- }
Filter:实现了Filter接口即可,Filter之间的执行顺序通过order来实现,order的值越低越先执行
- @Bean
- public FilterRegistrationBean<Filter> filterRegistrationBean1()
- {
- FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
- filterRegistrationBean.setFilter(new FilterJavaConfigTest2());
- filterRegistrationBean.setUrlPatterns(Arrays.asList("/filter/test/*"));
- filterRegistrationBean.setOrder(1);
- return filterRegistrationBean;
- }
- @Bean
- public FilterRegistrationBean<Filter> filterRegistrationBean2()
- {
- FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
- filterRegistrationBean.setFilter(new FilterJavaConfigTest3());
- filterRegistrationBean.setUrlPatterns(Arrays.asList("/filter/test/*"));
- filterRegistrationBean.setOrder(2);
- return filterRegistrationBean;
- }
Listener: 根据需求实现不同的listener接口即可
- @Bean
- public ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean()
- {
- ServletListenerRegistrationBean<EventListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
- servletListenerRegistrationBean.setListener(new ListenerTest1());
- return servletListenerRegistrationBean;
- }
Interceptor:与上面写的那种Interceptor相同
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------至于常见的SSM项目,重点是在web.xml,除了Interceptor
PS:因为本人搭建SSM项目更喜欢于采用JavaConfig的形式,怕麻烦,哈哈(不不不,该说SSM项目搭建有点烦),所以此处直接随意新建了个web.xml来代替
Servlet:
- <servlet>
- <servlet-name>test3</servlet-name>
- <servlet-class>com.test.servlet.ServletJavaConfigTest3</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>test3</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
Listener:
- <listener>
- <listener-class>com.test.listener.ListenerTest1</listener-class>
- </listener>
Filter:
- <filter>
- <filter-name>ss</filter-name>
- <filter-class>com.test.filter.AnnotationFilterTest1</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>ss</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
Interceptor是特殊的存在,他是基于SpringMVC的,所以要在Spring的配置文件中配置,但是本人更喜欢JavaConfig的形式,所以更喜欢:
- @Configuration
- public class InterceptroConfiguration implements WebMvcConfigurer
- {
- @Override
- public void addInterceptors(InterceptorRegistry registry)
- {
- registry.addInterceptor(new MyInterceptorTest1()).addPathPatterns("/interceptor/*");
- registry.addInterceptor(new MyInterceptorTest2()).addPathPatterns("/interceptor/33");
- }
- }
而非: (随个人喜好!!!!!!!!!!!!!!!!!!!!!!!!!)
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mapping path="/admin"/>
- <bean class="com.test.blog.Interceptor.SecurityInterceptor"/>
- </mvc:interceptor>
- <mvc:interceptor>
- <mvc:mapping path="/admin/**"/>
- <bean class="com.test.blog.Interceptor.SecurityInterceptor"/>
- </mvc:interceptor>
- </mvc:interceptors>