最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot注冊web組件的實(shí)現(xiàn)方式

 更新時(shí)間:2023年10月06日 09:01:22   作者:會洗碗的CV工程師  
Servlet是Java Web應(yīng)用程序的基礎(chǔ),它提供了處理客戶端請求的機(jī)制,Servlet三大組件是指Servlet、Filter和Listener,它們是Java Web應(yīng)用程序的核心組件,本文將給大家介紹一下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ǔ),它是一個(gè)Java類,用于處理客戶端請求并生成響應(yīng)。Servlet可以通過注解或web.xml文件進(jìn)行配置,它通常用于處理HTTP請求和響應(yīng)。
  • Filter:Filter是一個(gè)Java類,用于攔截和處理客戶端請求和響應(yīng)。Filter可以在請求到達(dá)Servlet之前或響應(yīng)返回客戶端之前執(zhí)行預(yù)處理和后處理操作,例如驗(yàn)證用戶身份、編碼解碼、壓縮和解壓縮等。
  • Listener:Listener是一個(gè)Java類,用于監(jiān)聽Web應(yīng)用程序中的事件,并在事件發(fā)生時(shí)執(zhí)行相應(yīng)的操作。Listener可以通過注解或web.xml文件進(jìn)行配置,它通常用于處理應(yīng)用程序啟動、停止、會話創(chuàng)建和銷毀等事件。

總之,Servlet三大組件是Java Web應(yīng)用程序的核心組件,它們分別用于處理請求、攔截請求和監(jiān)聽事件,從而實(shí)現(xiàn)了一個(gè)完整的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);
    }
}

這里我們需要新建一個(gè)配置類,將該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);
    }
}

此時(shí)如果我們直接運(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)了?。?! 

以上就是SpringBoot注冊web組件的實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot注冊web組件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java運(yùn)算符>、>>、>>>三者的區(qū)別

    Java運(yùn)算符>、>>、>>>三者的區(qū)別

    這篇文章主要介紹了Java運(yùn)算符>、>>、>>>三者的區(qū)別,做了一個(gè)簡單的對比,并用實(shí)例說明,需要的朋友可以參考下
    2014-06-06
  • Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題

    Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題

    在使用Postman進(jìn)行接口測試時(shí),對于POST請求,需將請求頭設(shè)置為application/x-www-form-urlencoded,并將參數(shù)轉(zhuǎn)為String類型,通常在GET請求中,參數(shù)直接拼接在URL后,本文通過具體實(shí)例,詳細(xì)講解了參數(shù)處理的方法,適合API開發(fā)者參考
    2024-09-09
  • Java遞歸實(shí)現(xiàn)菜單樹的方法詳解

    Java遞歸實(shí)現(xiàn)菜單樹的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java遞歸實(shí)現(xiàn)菜單樹的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • java  HashMap擴(kuò)容詳解及實(shí)例代碼

    java HashMap擴(kuò)容詳解及實(shí)例代碼

    這篇文章主要介紹了java HashMap擴(kuò)容詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • spring-boot-maven-plugin插件爆紅問題及解決方案

    spring-boot-maven-plugin插件爆紅問題及解決方案

    這篇文章主要介紹了spring-boot-maven-plugin插件爆紅問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Java項(xiàng)目--家庭收支記錄程序

    Java項(xiàng)目--家庭收支記錄程序

    本文主要介紹Java基礎(chǔ)階段的一個(gè)小項(xiàng)目——家庭收支記錄程序(附完整源代碼),本項(xiàng)目所用到的主要知識點(diǎn):基本語法、數(shù)組和方法。本項(xiàng)目并不難,主要是對Java初學(xué)者的基礎(chǔ)綜合運(yùn)用的訓(xùn)練及檢驗(yàn)
    2021-07-07
  • 如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能

    如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能

    在做項(xiàng)目時(shí),后端經(jīng)常采用上傳文件組件MultipartFile,下面這篇文章主要給大家介紹了關(guān)于如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式

    SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式

    這篇文章主要介紹了SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Spring為何要用三級緩存來解決循環(huán)依賴問題

    Spring為何要用三級緩存來解決循環(huán)依賴問題

    這篇文章主要給大家介紹了關(guān)于Spring為何要用三級緩存來解決循環(huán)依賴問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java正則表達(dá)式工具方法匯總

    Java正則表達(dá)式工具方法匯總

    這篇文章主要介紹了Java正則表達(dá)式工具方法匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評論

夹江县| 松江区| 香格里拉县| 昌都县| 澜沧| 宁安市| 许昌市| 融水| 孝感市| 乌兰察布市| 同江市| 裕民县| 太白县| 青神县| 环江| 兴文县| 铁岭市| 新宁县| 怀宁县| 哈尔滨市| 伽师县| 丹阳市| 盖州市| 林芝县| 海伦市| 定南县| 安化县| 枝江市| 彭阳县| 临汾市| 武隆县| 益阳市| 陆丰市| 霍山县| 化德县| 舒城县| 平定县| 九龙县| 讷河市| 布拖县| 和顺县|