欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Java Web三大件的詳細過程

 更新時間:2025年04月21日 11:21:09   作者:axinawang  
這篇文章主要介紹了SpringBoot整合Java Web三大件的詳細過程,注冊自定義的Servlet、Filter、Listener組件到springboot內(nèi)嵌的Servlet容器,讓它們發(fā)揮自己的作用,需要的朋友可以參考下

目的:注冊自定義的Servlet、Filter、Listener組件到springboot內(nèi)嵌的Servlet容器,讓它們發(fā)揮自己的作用

使用Spring Bean 注冊Java Web三大組件

路徑掃描整合javaweb三大組件

1.三大組件上添加對應(yīng)注解

在對應(yīng)組件上分別使用@WebServlet(“/annotationServlet”)注解來映射“/annotationServlet”請求的Servlet類,

使用@WebFilter(value = {“/antionLogin”,“/antionMyFilter”})注解來映射“/antionLogin”和“/antionMyFilter”請求的Filter類,

使用@WebListener注解來標(biāo)注Listener類。

@WebServlet("/annotationServlet")
public class MyServlet extends HttpServlet {
@WebFilter(value = {"/antionLogin","/antionMyFilter"})
public class MyFilter implements Filter {
@WebListener
public class MyListener implements ServletContextListener {

2.主程序啟動類上添加@ServletComponentScan注解,開啟基于注解方式的Servlet組件掃描支持

@ServletComponentScan
@SpringBootApplication
public class MyChapter05Application

3.測試

http://localhost:8080/annotationServlet

http://localhost:8080/antionLogin

http://localhost:8080/antionMyFilter

使用RegistrationBean注冊Java Web三大件

使用組件注冊方式整合Servlet

1.創(chuàng)建component子包及一個自定義Servlet類MyServlet,使用@Component注解將MyServlet類作為組件注入Spring容器。MyServlet類繼承自HttpServlet,通過HttpServletResponse對象向頁面輸出“hello MyServlet”。

@Component
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().write("hello MyServlet");
    }
}

2.在config子包下創(chuàng)建Servlet組件配置類ServletConfig,來注冊Servlet組件

@Configuration
public class ServletConfig {
    // 注冊servlet組件
    @Bean
    public ServletRegistrationBean getServlet(MyServlet myServlet) {
        ServletRegistrationBean registrationBean =
                new ServletRegistrationBean(myServlet, "/myServlet");
        return registrationBean;
    }
}

3.重啟項目,啟動成功后,在瀏覽器上訪問http://localhost:8080/myServlet

使用組件注冊方式整合Filter

1.在component包下創(chuàng)建一個自定義Filter類MyFilter,使用@Component注解將當(dāng)前MyFilter類作為組件注入到Spring容器中。MyFilter類實現(xiàn)Filter接口,并重寫了init()、doFilter()和destroy()方法,在doFilter()方法中向控制臺打印了“hello MyFilter”字符串。

@Component
public class MyFilter implements Filter {
	@Override 
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("hello my filter!"); 
		chain.doFilter(request, response);
	}
}

2.向Servlet組件配置類注冊自定義Filter類

@Bean                                                                            
public FilterRegistrationBean getFilter(MyFilter filter){                        
	FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
	//過濾出請求路徑"/toLoginPage","/myFilter",對它們特殊處理,也就是執(zhí)行Filter中的方法。                 
	registrationBean.setUrlPatterns(Arrays.asList("/toLoginPage","/myFilter"));  
	return registrationBean;                                                     
}                                                                                

3、項目啟動成功后,在瀏覽器上訪問http://localhost:8080/myFilter,查看控制臺打印效果

使用組件注冊方式整合Listener

1.創(chuàng)建一個類MyListener

@Component
public class MyListener implements ServletContextListener {
    @Override 
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("contextInitialized ...");
    }
    @Override 
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("contextDestroyed ...");
    }
}

2.向Servlet組件配置類注冊自定義Listener類

@Bean                                                                            
public ServletListenerRegistrationBean getServletListener(MyListener myListener){
	ServletListenerRegistrationBean registrationBean =                           
			new ServletListenerRegistrationBean(myListener);                     
	return registrationBean;                                                     
}                                                                                

3.項目啟動成功后查看控制臺打印效果

contextInitialized ...

4、正常關(guān)閉(保證是正常啟動的)

步驟:
        ①`pom.xml`添加依賴:

<dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>

        ②配置文件`application.properties`:

            #開啟所有的端點
            management.endpoints.web.exposure.include=*
            #啟用shutdown
            management.endpoint.shutdown.enabled=true

        ③執(zhí)行關(guān)閉請求(POST):
            在開發(fā)者工具的console輸入如下代碼,然后按回車:

fetch(new Request('http://localhost:8081/actuator/shutdown',{method:'POST'})).then((resp)=>{console.log(resp)})

結(jié)果:

到此這篇關(guān)于SpringBoot整合Java Web三大件的文章就介紹到這了,更多相關(guān)SpringBoot整合Java Web內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合Swagger頁面禁止訪問swagger-ui.html方式

    SpringBoot整合Swagger頁面禁止訪問swagger-ui.html方式

    本文介紹了如何在SpringBoot項目中通過配置SpringSecurity和創(chuàng)建攔截器來禁止訪問SwaggerUI頁面,此外,還提供了禁用SwaggerUI和Swagger資源的配置方法,以確保這些端點和頁面對外部用戶不可見或無法訪問
    2025-02-02
  • Spring中@Cacheable注解的使用詳解

    Spring中@Cacheable注解的使用詳解

    這篇文章主要介紹了Spring中@Cacheable注解的使用詳解,Spring框架提供了@Cacheable注解來輕松地將方法結(jié)果緩存起來,以便在后續(xù)調(diào)用中快速訪問,本文將詳細介紹@Cacheable注解的使用方法,并從源碼級別解析其實現(xiàn)原理,需要的朋友可以參考下
    2023-11-11
  • Java 程序內(nèi)部是如何執(zhí)行的?

    Java 程序內(nèi)部是如何執(zhí)行的?

    這篇文章主要介紹了Java 程序內(nèi)部是如何執(zhí)行的,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 基于springboot i18n國際化后臺多種語言設(shè)置的方式

    基于springboot i18n國際化后臺多種語言設(shè)置的方式

    這篇文章主要介紹了基于springboot i18n國際化后臺多種語言設(shè)置的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Mybatis防止sql注入的實例

    Mybatis防止sql注入的實例

    本文通過實例給大家介紹了Mybatis防止sql注入的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-06-06
  • java之使用多線程代替for循環(huán)(解決主線程提前結(jié)束問題)

    java之使用多線程代替for循環(huán)(解決主線程提前結(jié)束問題)

    這篇文章主要介紹了java之使用多線程代替for循環(huán)(解決主線程提前結(jié)束問題),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 詳解Java中的時區(qū)類TimeZone的用法

    詳解Java中的時區(qū)類TimeZone的用法

    TimeZone可以用來獲取或者規(guī)定時區(qū),也可以用來計算時差,這里我們就來詳解Java中的時區(qū)類TimeZone的用法,特別要注意下面所提到的TimeZone相關(guān)的時間校準問題.
    2016-06-06
  • 在Spring?Boot使用Undertow服務(wù)的方法

    在Spring?Boot使用Undertow服務(wù)的方法

    Undertow是RedHAT紅帽公司開源的產(chǎn)品,采用JAVA開發(fā),是一款靈活,高性能的web服務(wù)器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默認Web容器,這篇文章給大家介紹了在Spring?Boot使用Undertow服務(wù)的方法,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • 使用@Order控制配置類/AOP/方法/字段的加載順序詳解

    使用@Order控制配置類/AOP/方法/字段的加載順序詳解

    這篇文章主要介紹了使用@Order控制配置類/AOP/方法/字段的加載順序詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java通過jersey實現(xiàn)客戶端圖片上傳示例

    Java通過jersey實現(xiàn)客戶端圖片上傳示例

    本篇文章主要介紹了Java通過jersey實現(xiàn)客戶端圖片上傳示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03

最新評論