springboot注册Servlet、Filter、Listener的方式

方式一:注解

@WebServlet

@WebFilter

@WebListener

在实现类上使用该注解即可一键注册

方式二:配置类

在@Configuration标识的配置类中通过RegistrationBean进行注册

@Bean
public ServletRegistrationBean regServlet() {
    ServletRegistrationBean userServlet= new ServletRegistrationBean();
    userServlet.addUrlMappings("/servlet");
    userServlet.setServlet(new UserServlet());
    return userServlet;
 
}
@Bean
  public FilterRegistrationBean regFilter() {
    FilterRegistrationBean userFilter = new FilterRegistrationBean();
    userFilter .addUrlPatterns("/*");
    userFilter .setFilter(new UserFilter ());
    return userFilter ;
 
}
@Bean
  public ServletListenerRegistrationBean<LoginSessionListener> regServletListener() {
    ServletListenerRegistrationBean<LoginSessionListener> loginSessionListener= new ServletListenerRegistrationBean<LoginSessionListener>();
    loginSessionListener.setListener(new LoginSessionListener());
    return loginSessionListener;
 
}

 

posted @ 2023-03-30 15:04  少年阿丁  阅读(13)  评论(0编辑  收藏  举报