SpringBoot注冊(cè)web組件的實(shí)現(xiàn)方式
前言
Servlet是Java Web應(yīng)用程序的基礎(chǔ),它提供了處理客戶端請(qǐng)求的機(jī)制。Servlet三大組件是指Servlet、Filter和Listener,它們是Java Web應(yīng)用程序的核心組件。
- Servlet:Servlet是Java Web應(yīng)用程序的基礎(chǔ),它是一個(gè)Java類,用于處理客戶端請(qǐng)求并生成響應(yīng)。Servlet可以通過(guò)注解或web.xml文件進(jìn)行配置,它通常用于處理HTTP請(qǐng)求和響應(yīng)。
- Filter:Filter是一個(gè)Java類,用于攔截和處理客戶端請(qǐng)求和響應(yīng)。Filter可以在請(qǐng)求到達(dá)Servlet之前或響應(yīng)返回客戶端之前執(zhí)行預(yù)處理和后處理操作,例如驗(yàn)證用戶身份、編碼解碼、壓縮和解壓縮等。
- Listener:Listener是一個(gè)Java類,用于監(jiān)聽(tīng)Web應(yīng)用程序中的事件,并在事件發(fā)生時(shí)執(zhí)行相應(yīng)的操作。Listener可以通過(guò)注解或web.xml文件進(jìn)行配置,它通常用于處理應(yīng)用程序啟動(dòng)、停止、會(huì)話創(chuàng)建和銷毀等事件。
總之,Servlet三大組件是Java Web應(yīng)用程序的核心組件,它們分別用于處理請(qǐng)求、攔截請(qǐng)求和監(jiān)聽(tīng)事件,從而實(shí)現(xiàn)了一個(gè)完整的Java Web應(yīng)用程序。
一、注冊(cè)Servlet組件
由于SpringBoot項(xiàng)目沒(méi)有web.xml文件,所以無(wú)法在web.xml中注冊(cè)web組件,SpringBoot有自己的方式注冊(cè)web組件。
1.1 使用SpringBoot注解加繼承HttpServet類注冊(cè)
編寫(xiě)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);
}
}啟動(dòng)類添加掃描Web組件用到的注解@ServletComponentScan

OK,直接運(yùn)行看效果:訪問(wèn):http://localhost:8080/first
是可以直接打印出來(lái)的。

1.2 通過(guò)繼承HttpServet類加配置類來(lái)進(jìn)行注冊(cè)
ok,接下來(lái)我們講解第二種注冊(cè)方式,通過(guò)繼承HttpServet類來(lái)進(jìn)行注冊(cè),代碼如下:
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可以注冊(cè)Servlet組件,將其放入Spring容器中即可注冊(cè)Servlet
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
// 注冊(cè)Servlet組件
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
// 將Servlet組件添加訪問(wèn)路徑
bean.addUrlMappings("/second");
return bean;
}
}OK,然后我們點(diǎn)擊運(yùn)行,直接訪問(wèn)http://localhost:8080/second

因此說(shuō)明了兩種注冊(cè)方式都是可用的。
二、注冊(cè)Listener組件
2.1 使用SpringBoot注解和實(shí)現(xiàn)ServletContextListener接口注冊(cè)
原理和上面一樣只不過(guò)是直接注解,無(wú)需配置類,代碼如下
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)目就會(huì)打印上面那句話,接下來(lái)我們直接運(yùn)行項(xiàng)目,看看是否打印,

說(shuō)明該方法是可行的,
2.2 ServletContextListener接口和配置類來(lái)進(jìn)行注冊(cè)
同樣我們?nèi)サ糇⒔馐褂门渲妙愖?cè) ,代碼如下:
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可以注冊(cè)Servlet組件,將其放入Spring容器中即可注冊(cè)Servlet
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
// 注冊(cè)Listener組件
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
return bean;
}
}然后直接運(yùn)行看看結(jié)果,

OK,同樣也是沒(méi)有問(wèn)題的。
三、注冊(cè)Filter組件
3.1 使用SpringBoot注解加實(shí)現(xiàn)Filter接口注冊(cè)
和上面一樣,代碼如下:
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();
}
}這里訪問(wèn)/frist的話/我們要輸入true才進(jìn)行放行,看看是否成功過(guò)濾,

OK,這里是成功了的,接下來(lái)也是一樣的操作
3.2 通過(guò)實(shí)現(xiàn)Filter接口和配置類來(lái)進(jìn)行注冊(cè)通過(guò)實(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可以注冊(cè)Servlet組件,將其放入Spring容器中即可注冊(cè)Servlet
@Bean
public FilterRegistrationBean getFilterRegistrationBean (){
// 注冊(cè)filter組件
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
// 添加過(guò)濾路徑
bean.addUrlPatterns("/second");
return bean;
}
}同樣這里訪問(wèn)/second的話/我們要輸入true才進(jìn)行放行,看看是否成功過(guò)濾,

OK,這里已經(jīng)成功實(shí)現(xiàn)了?。。?nbsp;
以上就是SpringBoot注冊(cè)web組件的實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot注冊(cè)web組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn)
這篇文章主要介紹了Idea配置Maven阿里云鏡像加速的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot啟動(dòng)及自動(dòng)裝配原理過(guò)程詳解
這篇文章主要介紹了SpringBoot啟動(dòng)及自動(dòng)裝配原理過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
如何使用Collections.reverse對(duì)list集合進(jìn)行降序排序
這篇文章主要介紹了Java使用Collections.reverse對(duì)list集合進(jìn)行降序排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java 8 中的 10 個(gè)特性總結(jié)及詳解
本主要介紹Java 8中的新特性,這里整理了相關(guān)資料并整理了10個(gè)特性,逐一介紹說(shuō)明,有興趣的朋友可以參考下2016-09-09
JDBC查詢Map轉(zhuǎn)對(duì)象實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了JDBC查詢Map轉(zhuǎn)對(duì)象實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
詳解Springboot應(yīng)用啟動(dòng)以及關(guān)閉時(shí)完成某些操作
這篇文章主要介紹了詳解Springboot應(yīng)用啟動(dòng)以及關(guān)閉時(shí)完成某些操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Spring實(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

