欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot如何注冊Servlet、Filter、Listener的幾種方式

 更新時間:2018年10月29日 13:47:22   作者:技術(shù)小能手  
在Servlet 3.0之前都是使用web.xml文件進(jìn)行配置,這篇文章主要介紹了SpringBoot如何注冊Servlet、Filter、Listener的幾種方式,在Servlet 3.0之前都是使用web.xml文件進(jìn)行配置,

在Servlet 3.0之前都是使用web.xml文件進(jìn)行配置,需要增加Servlet、Filter或者Listener都需要在web.xml增加相應(yīng)的配置。Servlet 3.0之后可以使用注解進(jìn)行配置Servlet、Filter或者Listener;springboot也提供了使用代碼進(jìn)行注冊Servlet、Filter或者Listener。所以springboot有兩種方式進(jìn)行Servlet、Filter或者Listener配置。

方式一:使用注解

(1)注冊Servlet

使用@WebServlet注冊,需要在Servlet類上使用該注解即可,但是需要在@Configuration類中使用Spring Boot提供的注解@ServletComponentScan掃描注冊相應(yīng)的Servlet。

(2)  注冊Filter

使用@WebFilter注冊,需要在Filter類上使用該注解即可,但是需要在@Configuration類中使用Spring Boot提供的注解@ServletComponentScan掃描注冊相應(yīng)的Filter。

(3)注冊Listener

使用@WebListener注冊,需要在Filter類上使用該注解即可,但是需要在@Configuration類中使用Spring Boot提供的注解@ServletComponentScan掃描注冊相應(yīng)的Listener。

方式二:使用spring提供的方式

(1)注冊Servlet

使用ServletRegistrationBean注冊只需要在@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 FilterRegistrationBean regFilter() {
    FilterRegistrationBean userFilter = new FilterRegistrationBean();
    userFilter .addUrlPatterns("/*");
    userFilter .setFilter(new UserFilter ());
    return userFilter ;

}

(3)注冊Listener

使用ServletListenerRegistrationBean注冊Listener只需要在@Configuration類中加入類似以下的代碼:

@Bean
  public ServletListenerRegistrationBean<LoginSessionListener> regServletListener() {
    ServletListenerRegistrationBean<LoginSessionListener> loginSessionListener= new ServletListenerRegistrationBean<LoginSessionListener>();
    loginSessionListener.setListener(new LoginSessionListener());
    return loginSessionListener;

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論