Flask接口签名sign原理与实例代码浅析
280
2023-01-20
SpringBoot如何注册Servlet、Filter、Listener的几种方式
在Servlet 3.0之前都是使用web.xml文件进行配置,需要增加Servlet、Filter或者Listener都需要在web.xml增加相应的配置。Servlet 3.0之后可以使用注解进行配置Servlet、Filter或者Listener;springboot也提供了使用代码进行注册Servlet、Filter或者Listener。所以springboot有两种方式进行Servlet、Filter或者Listener配置。
方式一:使用注解
(1)注册Servlet
使用@WebServlet注册,需要在Servlet类上使用该注解即可,但是需要在@Configuration类中使用Spring Boot提供的注解@ServletComponentScan扫描注册相应的Servlet。
(2) 注册Filter
使用@WebFilter注册,需要在Filter类上使用该注解即可,但是需要在@Configuration类中使用Spring Boot提供的注解@ServletComponentScan扫描注册相应的Filter。
(3)注册Listener
使用@WebListener注册,需要在Filter类上使用该注解即可,但是需要在@Configuration类中使用Spring Boot提供的注解@ServletComponentScan扫描注册相应的Listener。
方式二:使用spring提供的方式
(1)注册Servlet
使用ServletRegistrationBetDaBFrean注册只需要在@Configuration类中加入类似以下的代码
@Bean
public ServletRegistrationBean regServlet() {
ServletRegistrationBean userServlet= new ServletRegistrationBean();
userServlet.addUrlMappings("/servlet");
userServlet.setServlet(new UserServlet());
return userServlet;
}
(2) 注册Filter
使用FilterRegistrationBean注册Filter,只需要在@Configuration类中加入类似以下的代码:
@Bean
public FiletDaBFrterRegistrationBean regFilter() {
FilterRegistrationBean userFilter = new FilterRegistrationBean();
userFilter .addUrlPatterns("/*");
userFilter .setFilter(new UserFilter ());
return userFilter ;
}
http://
(3)注册Listener
使用ServletListenerRegistrationBean注册Listener只需要在@Configuration类中加入类似以下的代码:
@Bean
public ServletListenerRegistrationBean
ServletListenerRegistrationBean
loginSessionListener.setListener(new LoginSessionListener());
return loginSessionListener;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~