您好,登錄后才能下訂單哦!
小編這次要給大家分享的是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,就只能通過上邊這樣的方式來搞了。
二、測試
啟動服務,瀏覽器輸入"http://localhost:8083/servlet/test","http://localhost:8083/servlet/test2",查看console的輸出。
看完這篇關于springboot如何注入servlet的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。