問:有了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 @Override 15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 System.out.println("zhaojigang servlet"); 17 } 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 this.doGet(req, resp); 22 } 23 } View Code2、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 @Override 15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 System.out.println("zhaojigang servlet2"); 17 } 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 this.doGet(req, resp); 22 } 23 } View Code3、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 @Bean 11 public TestServlet testServlet(){ 12 return new TestServlet(); 13 } 14 15 @Bean 16 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 @Bean 24 public TestServlet2 testServlet2(){ 25 return new TestServlet2(); 26 } 27 28 @Bean 29 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的輸出。