springboot注入servlet的方法
問:有了springMVC,為什么還要用servlet?有了servlet3的注解,為什么還要使用ServletRegistrationBean注入的方式?
使用場景:在有些場景下,比如我們要使用hystrix-dashboard,這時候就需要注入HystrixMetricsStreamServlet(第三方的servlet),該servlet是hystrix的組件。
一、代碼
1、TestServlet(第一個servlet)
package com.xxx.secondboot.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { private static final long serialVersionUID = -4619665430596950563L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("zhaojigang servlet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
2、Testservlet2(第二個servlet)
package com.xxx.secondboot.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet2 extends HttpServlet { private static final long serialVersionUID = 3788279972938793265L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("zhaojigang servlet2"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
3、ServletConfig(servlet注入配置類)
package com.xxx.secondboot.servlet; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ServletConfig { @Bean public TestServlet testServlet(){ return new TestServlet(); } @Bean public ServletRegistrationBean testServletRegistrationBean(TestServlet testServlet){ ServletRegistrationBean registration = new ServletRegistrationBean(testServlet); registration.setEnabled(true); registration.addUrlMappings("/servlet/test"); return registration; } /********************************************/ @Bean public TestServlet2 testServlet2(){ return new TestServlet2(); } @Bean public ServletRegistrationBean test2ServletRegistrationBean(TestServlet2 testServlet2){ ServletRegistrationBean registration = new ServletRegistrationBean(testServlet2); registration.setEnabled(true); registration.addUrlMappings("/servlet/test2"); return registration; } }
說明:使用ServletRegistrationBean來注入servlet,對于每一個servlet都有一個ServletRegistrationBean來注入。
注意:如果只是自己要使用servlet,可以直接只用servlet3的注解來聲明servlet就好,但是像HystrixMetricsStreamServlet這樣的第三方servlet,就只能通過上邊這樣的方式來搞了。
二、測試
啟動服務(wù),瀏覽器輸入"http://localhost:8083/servlet/test","http://localhost:8083/servlet/test2",查看console的輸出。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 判斷一個數(shù)組中的數(shù)值是否連續(xù)相鄰的方法
下面小編就為大家分享一篇java 判斷一個數(shù)組中的數(shù)值是否連續(xù)相鄰的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03使用java8 API遍歷過濾文件目錄及子目錄和隱藏文件示例詳解
這篇文章主要介紹了使用java8API遍歷過濾文件目錄及子目錄及隱藏文件示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07MybatisPlus?BaseMapper?實現(xiàn)對數(shù)據(jù)庫增刪改查源碼
MybatisPlus?是一款在?Mybatis?基礎(chǔ)上進行的增強?orm?框架,可以實現(xiàn)不寫?sql?就完成數(shù)據(jù)庫相關(guān)的操作,這篇文章主要介紹了MybatisPlus?BaseMapper?實現(xiàn)對數(shù)據(jù)庫增刪改查源碼解析,需要的朋友可以參考下2023-01-01SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內(nèi)容
在本篇文章里小編給大家整理的是一篇關(guān)于SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內(nèi)容,有需要的朋友們參考下。2019-12-12使用arthas命令redefine實現(xiàn)Java熱更新(推薦)
今天分享一個非常重要的命令 redefine ,主要作用是加載外部的 .class 文件,用來替換 JVM 已經(jīng)加載的類,總結(jié)起來就是實現(xiàn)了 Java 的熱更新,感興趣的朋友跟隨小編一起看看吧2020-05-05