当前位置: 首页 > news >正文

SpringBoot之Web原生组件注入

文章目录

  • 前言
  • 一、原生注解方式注入
  • 二、Spring方式注入
  • 三、切换web服务器与定制化
  • 总结


前言

注入Web原生Servlet、Filter、Listeber以及切换Web服务器。


一、原生注解方式注入

官方文档 - Servlets, Filters, and listeners

Servlet注入:

@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("66666");}
}

Filter注入:

@Slf4j
@WebFilter(urlPatterns={"/css/*","/images/*"})
public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {log.info("MyFilter初始化完成");}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {log.info("MyFilter工作");chain.doFilter(request,response);}@Overridepublic void destroy() {log.info("MyFilter销毁");}
}

Listener注入:

@Slf4j
@WebListener
public class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {log.info("MySwervletContextListener监听到项目初始化完成");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {log.info("MySwervletContextListener监听到项目销毁");}
}

最后还要在主启动类添加注解@ServletComponentScan:

@SpringBootApplication
@ServletComponentScan(basePackages = "com.dragon.admin")//开启servlet注入组件扫描
public class Springboot4Application {public static void main(String[] args) {SpringApplication.run(Springboot4Application.class, args);}}

二、Spring方式注入

首先将上面的三个类上的注释都取消,作普通类。

ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean

@Configuration(proxyBeanMethods = true)
public class MyRegisterConfig {@Beanpublic ServletRegistrationBean myServlet(){Myservlet myservlet = new Myservlet();return new ServletRegistrationBean(myservlet,"/my","/my02");}@Beanpublic FilterRegistrationBean myFilter(){MyFilter myFilter = new MyFilter();
//        return new FilterRegistrationBean(myFilter,myServlet());FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));return filterRegistrationBean;}@Beanpublic ServletListenerRegistrationBean myListener(){MyServletContextListener myServletContextListener = new MyServletContextListener();return new ServletListenerRegistrationBean(myServletContextListener);}
}

三、切换web服务器与定制化

  • 默认支持的WebServer

    • Tomcat, Jetty, or Undertow
    • ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器。
  • 原理

    • SpringBoot应用启动发现当前是Web应用,web场景包-导入tomcat。
    • web应用会创建一个web版的IOC容器 ServletWebServerApplicationContext
    • ServletWebServerApplicationContext 启动的时候寻找 ServletWebServerFactory (Servlet 的web服务器工厂——>Servlet 的web服务器)。
    • SpringBoot底层默认有很多的WebServer工厂(ServletWebServerFactoryConfiguration内创建Bean),如:
      • TomcatServletWebServerFactory
      • JettyServletWebServerFactory
      • UndertowServletWebServerFactory
    • 底层直接会有一个自动配置类ServletWebServerFactoryAutoConfiguration
    • ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)。
    • ServletWebServerFactoryConfiguration 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
    • TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize——this.tomcat.start();
    • 内嵌服务器,与以前手动把启动服务器相比,改成现在使用代码启动(tomcat核心jar包存在)。

Spring Boot默认使用Tomcat服务器,若需更改其他服务器,则修改工程pom.xml:
这是修改成jetty的服务器

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

官方文档 - Use Another Web Server


总结

以上就是Web原生组件注入。

http://www.lryc.cn/news/179307.html

相关文章:

  • [每周一更]-(第64期):Dockerfile构造php定制化镜像
  • 若依不分离+Thymeleaf select选中多个回显
  • OCX 添加方法和事件 HTML调用ocx函数及回调 ocx又调用dll VS2017
  • 苹果iPhone手机使用草柴返利APP查询领取淘宝天猫京东优惠券如何取消关闭粘贴商品链接时的弹窗提示?
  • 主机安装elasticsearch后无法登陆
  • 【面试题精讲】JavaSe和JavaEE的区别
  • React 全栈体系(十五)
  • 【逆向】(c++)分析pe结构,拉伸pe结构,缩小pe结构
  • PyTorch实战:常用卷积神经网络搭建结构速览
  • 排序算法之【快速排序】
  • 声明式调用 —— SpringCloud OpenFeign
  • LuatOS-SOC接口文档(air780E)-- fota - 底层固件升级
  • 第二章 Introduction
  • WebGL 渲染三维图形作为纹理贴到另一个三维物体表面
  • 国庆《乡村振兴战略下传统村落文化旅游设计》许少辉八一新书行将售罄
  • Source Insight 工具栏图标功能介绍
  • 模板与泛型编程-函数模板
  • 了解ActiveMQ、RabbitMQ、RocketMQ和Kafka的特点
  • 第七章 用户和组管理
  • 给奶牛做直播之三
  • 【Java 进阶篇】MySQL 数据控制语言(DCL):管理用户权限
  • WPF 03
  • Android 使用kotlin+注解+反射+泛型实现MVP架构
  • 数据结构——堆(C语言)
  • B058-SpringBoot
  • 龙迅LT9611UXC 2PORT MIPICSI/DSI转HDMI(2.0)转换器+音频,内置MCU
  • STM32存储左右互搏 I2C总线读写FRAM MB85RC1M
  • 1340. 跳跃游戏 V;2039. 网络空闲的时刻;2767. 将字符串分割为最少的美丽子字符串
  • ElementUI之CUD+表单验证
  • Linux:nginx---web文件服务器