第二十四章 springboot注入servlet

问:有了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 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     @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 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     @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的输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值