新聞中心
在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 ServletListenerRegistrationBeanregServletListener() { 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


咨詢
建站咨詢
