第二十四章 springboot注入servlet,springbootservlet

來源:互聯網
上載者:User

第二十四章 springboot注入servlet,springbootservlet

問:有了springMVC,為什麼還要用servlet?有了servlet3的註解,為什麼還要使用ServletRegistrationBean注入的方式?

使用情境:在有些情境下,比如我們要使用hystrix-dashboard,這時候就需要注入HystrixMetricsStreamServlet(第三方的servlet),該servlet是hystrix的組件。

一、代碼

1、TestServlet(第一個servlet)

1 package com.xxx.secondboot.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class TestServlet extends HttpServlet {11 12 private static final long serialVersionUID = -4619665430596950563L;13 14 @Override15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {16 System.out.println("zhaojigang servlet");17 }18 19 @Override20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {21 this.doGet(req, resp);22 }23 }View Code

2、Testservlet2(第二個servlet)

1 package com.xxx.secondboot.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class TestServlet2 extends HttpServlet {11 12 private static final long serialVersionUID = 3788279972938793265L;13 14 @Override15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {16 System.out.println("zhaojigang servlet2");17 }18 19 @Override20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {21 this.doGet(req, resp);22 }23 }View Code

3、ServletConfig(servlet注入配置類)

 1 package com.xxx.secondboot.servlet; 2  3 import org.springframework.boot.context.embedded.ServletRegistrationBean; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6  7 @Configuration 8 public class ServletConfig { 9     10     @Bean11     public TestServlet testServlet(){12         return new TestServlet();13     }14     15     @Bean16     public ServletRegistrationBean testServletRegistrationBean(TestServlet testServlet){17         ServletRegistrationBean registration = new ServletRegistrationBean(testServlet);18         registration.setEnabled(true);19         registration.addUrlMappings("/servlet/test");20         return registration;21     }22     /********************************************/23     @Bean24     public TestServlet2 testServlet2(){25         return new TestServlet2();26     }27     28     @Bean29     public ServletRegistrationBean test2ServletRegistrationBean(TestServlet2 testServlet2){30         ServletRegistrationBean registration = new ServletRegistrationBean(testServlet2);31         registration.setEnabled(true);32         registration.addUrlMappings("/servlet/test2");33         return registration;34     }35     36 }

說明:使用ServletRegistrationBean來注入servlet,對於每一個servlet都有一個ServletRegistrationBean來注入。

注意:如果只是自己要使用servlet,可以直接只用servlet3的註解來聲明servlet就好,但是像HystrixMetricsStreamServlet這樣的第三方servlet,就只能通過上邊這樣的方式來搞了。

 

二、測試

啟動服務,瀏覽器輸入"http://localhost:8083/servlet/test","http://localhost:8083/servlet/test2",查看console的輸出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.