eclipse 使用maven 构建springboot+注入servlet
来源:互联网 发布:查看端口占用 编辑:程序博客网 时间:2024/06/05 23:54
本文转载至:http://www.cnblogs.com/java-zhao/p/5775103.html
问:有了springMVC,为什么还要用servlet?有了servlet3的注解,为什么还要使用ServletRegistrationBean注入的方式?
使用场景:在有些场景下,比如我们要使用hystrix-dashboard,这时候就需要注入HystrixMetricsStreamServlet(第三方的servlet),该servlet是hystrix的组件。
一、代码
1、TestServlet(第一个servlet)
package com.zzg.springbootone.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("zhouzhigang servlet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
2、Testservlet2(第二个servlet)
package com.zzg.springbootone.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("zhouzhigang servlet2"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}3、ServletConfig(servlet注入配置类)
package com.zzg.springbootone.servlet;import org.springframework.boot.context.embedded.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic 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:8080/servlet/test","http://localhost:8080/servlet/test2",查看console的输出。
截图如下: 0 0
- eclipse 使用maven 构建springboot+注入servlet
- eclipse 使用maven 构建springboot 程序
- eclipse 使用maven构建 springboot +swagger
- eclipse 使用maven 构建 springboot+mybatis
- eclipse 使用maven 构建springboot +log4j
- eclipse 使用maven 构建springboot +lombok
- eclipse 使用maven构建springboot+定时任务
- eclipse 使用maven 构建springboot+mybatis+多数据源+aop
- eclipse 使用maven 构建springboot 读取多个配置文件
- 使用Maven 构建 Servlet
- eclipse 使用maven 构建springboot+dubbo+zookeeper 构建soa服务(面向服务架构)
- eclipse 使用maven 构建springboot +全局异常与局部异常区别
- SpringBoot:servlet注入
- springboot之servlet注入
- 使用Eclipse 构建Maven项目
- 使用Eclipse构建Maven项目
- 使用Eclipse构建Maven项目
- 使用Eclipse构建Maven项目
- CUDA之Dynamic Parallelism详解(二)
- zoomeye用户使用手册
- css Sprite
- nodejs:使用emailjs发送邮件
- Renting Bikes 二分
- eclipse 使用maven 构建springboot+注入servlet
- 1. thinkphp相关
- source 命令找不到的情况(修改完/etc/profile文件之后,使其生效)
- OPENGL编程练习
- Linux权限的简单用法(使用者、文件、目录)
- Linux shell编程之输入输出重定向上篇
- iOS开发-三种倒计时的写法
- JSP页面编码设置
- 移动端真机调试方法介绍