SpringBoot注冊web組件的實(shí)現(xiàn)方式
前言
Servlet是Java Web應(yīng)用程序的基礎(chǔ),它提供了處理客戶端請求的機(jī)制。Servlet三大組件是指Servlet、Filter和Listener,它們是Java Web應(yīng)用程序的核心組件。
- Servlet:Servlet是Java Web應(yīng)用程序的基礎(chǔ),它是一個Java類,用于處理客戶端請求并生成響應(yīng)。Servlet可以通過注解或web.xml文件進(jìn)行配置,它通常用于處理HTTP請求和響應(yīng)。
- Filter:Filter是一個Java類,用于攔截和處理客戶端請求和響應(yīng)。Filter可以在請求到達(dá)Servlet之前或響應(yīng)返回客戶端之前執(zhí)行預(yù)處理和后處理操作,例如驗(yàn)證用戶身份、編碼解碼、壓縮和解壓縮等。
- Listener:Listener是一個Java類,用于監(jiān)聽Web應(yīng)用程序中的事件,并在事件發(fā)生時執(zhí)行相應(yīng)的操作。Listener可以通過注解或web.xml文件進(jìn)行配置,它通常用于處理應(yīng)用程序啟動、停止、會話創(chuàng)建和銷毀等事件。
總之,Servlet三大組件是Java Web應(yīng)用程序的核心組件,它們分別用于處理請求、攔截請求和監(jiān)聽事件,從而實(shí)現(xiàn)了一個完整的Java Web應(yīng)用程序。
一、注冊Servlet組件
由于SpringBoot項(xiàng)目沒有web.xml文件,所以無法在web.xml中注冊web組件,SpringBoot有自己的方式注冊web組件。
1.1 使用SpringBoot注解加繼承HttpServet類注冊
編寫servlet,首先是要添加@WebServlet注解;代碼如下:
package com.example.demo.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/first") public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("First Servlet......"); super.doGet(req, resp); } }
啟動類添加掃描Web組件用到的注解@ServletComponentScan
OK,直接運(yùn)行看效果:訪問:http://localhost:8080/first
是可以直接打印出來的。
1.2 通過繼承HttpServet類加配置類來進(jìn)行注冊
ok,接下來我們講解第二種注冊方式,通過繼承HttpServet類來進(jìn)行注冊,代碼如下:
package com.example.demo.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Second Servlet........"); super.doGet(req, resp); } }
這里我們需要新建一個配置類,將該Servlet加載到Spring容器中,配置類代碼如下
package com.example.demo.config; import com.example.demo.servlet.SecondServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ServletConfig { // ServletRegistrationBean可以注冊Servlet組件,將其放入Spring容器中即可注冊Servlet @Bean public ServletRegistrationBean getServletRegistrationBean(){ // 注冊Servlet組件 ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); // 將Servlet組件添加訪問路徑 bean.addUrlMappings("/second"); return bean; } }
OK,然后我們點(diǎn)擊運(yùn)行,直接訪問http://localhost:8080/second
因此說明了兩種注冊方式都是可用的。
二、注冊Listener組件
2.1 使用SpringBoot注解和實(shí)現(xiàn)ServletContextListener接口注冊
原理和上面一樣只不過是直接注解,無需配置類,代碼如下
package com.example.demo.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener() public class FirstListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("FirsListener........."); } @Override public void contextDestroyed(ServletContextEvent sce) { ServletContextListener.super.contextDestroyed(sce); } }
此時如果我們直接運(yùn)行項(xiàng)目就會打印上面那句話,接下來我們直接運(yùn)行項(xiàng)目,看看是否打印,
說明該方法是可行的,
2.2 ServletContextListener接口和配置類來進(jìn)行注冊
同樣我們?nèi)サ糇⒔馐褂门渲妙愖?,代碼如下:
package com.example.demo.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class SecondListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("Second Listener..........."); } @Override public void contextDestroyed(ServletContextEvent sce) { ServletContextListener.super.contextDestroyed(sce); } }
配置類代碼入下:
package com.example.demo.config; import com.example.demo.listener.SecondListener; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ListenerConfig { // ServletRegistrationBean可以注冊Servlet組件,將其放入Spring容器中即可注冊Servlet @Bean public ServletListenerRegistrationBean getServletListenerRegistrationBean(){ // 注冊Listener組件 ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener()); return bean; } }
然后直接運(yùn)行看看結(jié)果,
OK,同樣也是沒有問題的。
三、注冊Filter組件
3.1 使用SpringBoot注解加實(shí)現(xiàn)Filter接口注冊
和上面一樣,代碼如下:
package com.example.demo.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; import java.util.Scanner; //@WebFilter("/first") public class FirstServletFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("進(jìn)入First Filter"); Scanner scanner = new Scanner(System.in); boolean flag = scanner.nextBoolean(); while(!flag){ System.out.println("要輸入true才放行?。?!"); flag = scanner.nextBoolean(); } filterChain.doFilter(servletRequest,servletResponse); scanner.close(); } @Override public void destroy() { Filter.super.destroy(); } }
這里訪問/frist的話/我們要輸入true才進(jìn)行放行,看看是否成功過濾,
OK,這里是成功了的,接下來也是一樣的操作
3.2 通過實(shí)現(xiàn)Filter接口和配置類來進(jìn)行注冊通過實(shí)現(xiàn)
代碼如下:
package com.example.demo.filter; import javax.servlet.*; import java.io.IOException; import java.util.Scanner; public class SecondFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Filter.super.init(filterConfig); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("進(jìn)入Second Filter"); Scanner scanner = new Scanner(System.in); boolean flag = scanner.nextBoolean(); while(!flag){ System.out.println("要輸入true才可以放行!?。?); flag = scanner.nextBoolean(); } filterChain.doFilter(servletRequest,servletResponse); scanner.close(); } @Override public void destroy() { Filter.super.destroy(); } }
配置類代碼如下:
package com.example.demo.config; import com.example.demo.filter.SecondFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FilterConfig { // ServletRegistrationBean可以注冊Servlet組件,將其放入Spring容器中即可注冊Servlet @Bean public FilterRegistrationBean getFilterRegistrationBean (){ // 注冊filter組件 FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); // 添加過濾路徑 bean.addUrlPatterns("/second"); return bean; } }
同樣這里訪問/second的話/我們要輸入true才進(jìn)行放行,看看是否成功過濾,
OK,這里已經(jīng)成功實(shí)現(xiàn)了?。。?nbsp;
以上就是SpringBoot注冊web組件的實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot注冊web組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn)
這篇文章主要介紹了Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04如何使用Collections.reverse對list集合進(jìn)行降序排序
這篇文章主要介紹了Java使用Collections.reverse對list集合進(jìn)行降序排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11JDBC查詢Map轉(zhuǎn)對象實(shí)現(xiàn)過程詳解
這篇文章主要介紹了JDBC查詢Map轉(zhuǎn)對象實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10詳解Springboot應(yīng)用啟動以及關(guān)閉時完成某些操作
這篇文章主要介紹了詳解Springboot應(yīng)用啟動以及關(guān)閉時完成某些操作,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作,結(jié)合實(shí)例形式分析了Spring協(xié)調(diào)作用域不同步的Bean相關(guān)配置及使用技巧,需要的朋友可以參考下2019-11-11