玩转Spring Boot 注册Servlet、Filter、Listener 原
欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/0824/108089.html
玩转Spring Boot 注册Servlet、Filter、Listener
JAVA架构师Ya七月 2019-08-23
玩转Spring Boot 注册Servlet、Filter、Listener
在Servlet 3.0之前我们都是使用web.xml进行配置,需要增加Servlet、Filter或者Listener都是在web.xml增加相应的配置即可。这里我们使用的是使用Java配置来注册Servlet、Filter、Listener。
1.注册Servlet
(1)使用ServletRegistrationBean注册
使用ServletRegistrationBean注册只需要在@Configuration类中加入即可,例如以下代码:
2)使用@WebServlet
使用@WebServlet注册,需要在Servlet类上使用该注解即可,但是需要在@Configuration类中使用Spring Boot提供的注解@ServletComponentScan扫描注册相应的Servlet。
2.注册Filter
(1)使用FilterRegistrationBean注册
使用FilterRegistrationBean注册Filter,只需要在@Configuration类中加入即可,例如以下代码:
2)使用@WebFilter
使用@WebFilter注册,需要在Filter类上使用该注解即可,但是需要在@Configuration类中使用Spring Boot提供的注解@ServletComponentScan扫描注册相应的Filter。
3.注册Listener
(1)使用ServletListenerRegistrationBean注册
使用ServletListenerRegistrationBean注册Listener只需要在@Configuration类中加入即可,例如以下代码:
(2)使用@WebListener
使用@WebListener注册,需要在Filter类上使用该注解即可,但是需要在@Configuration类中使用Spring Boot提供的注解@ServletComponentScan扫描注册相应的Listener。
——致词:关注转发收藏