玩转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。
——致词:关注转发收藏