日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot如何注冊(cè)Servlet、Filter、Listener的幾種方式

在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)行注冊(cè)Servlet、Filter或者Listener。所以springboot有兩種方式進(jìn)行Servlet、Filter或者Listener配置。

目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、咸陽網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

方式一:使用注解

(1)注冊(cè)Servlet

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

(2)  注冊(cè)Filter

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

(3)注冊(cè)Listener

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

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

(1)注冊(cè)Servlet

使用ServletRegistrationBean注冊(cè)只需要在@Configuration類中加入類似以下的代碼

@Bean
public ServletRegistrationBean regServlet() {
    ServletRegistrationBean userServlet= new ServletRegistrationBean();
    userServlet.addUrlMappings("/servlet");
    userServlet.setServlet(new UserServlet());
    return userServlet;

}

(2)  注冊(cè)Filter

使用FilterRegistrationBean注冊(cè)Filter,只需要在@Configuration類中加入類似以下的代碼:

@Bean
  public FilterRegistrationBean regFilter() {
    FilterRegistrationBean userFilter = new FilterRegistrationBean();
    userFilter .addUrlPatterns("/*");
    userFilter .setFilter(new UserFilter ());
    return userFilter ;

}

(3)注冊(cè)Listener

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

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

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


分享名稱:SpringBoot如何注冊(cè)Servlet、Filter、Listener的幾種方式
網(wǎng)站URL:http://www.dlmjj.cn/article/ijehjh.html