• springboot11:原生组件注入


    一.注入三大组件

    1.servlet API注入(即可处理)–没有经过springmvc的拦截器

    • @WebServlet(urlPatterns=“/my”)
      public class Myservlet extends HttpServlet
      重写doGet方法

    • 还需要在主配置类中加入@ServletComponentScan(要扫描的包)指定原生servlet组件放在哪, 会自动扫描我们加入的servlet组件

    2.Fileter注入

    • @WebFileter(urlPatterns=“/my”)
      public class MyFilter implements Filter
      重写doFilter方法

    3.listenr注入

    • @WebListener
      public class listenr implements ServletContextListener
      重写ContextInitialized(监听项目初始化)

    4.使用其他注解@Configuration

    @Configuration
    public class MyRegistConfig {
    
        @Bean
        public ServletRegistrationBean myServlet(){
            MyServlet myServlet = new MyServlet();
            return new ServletRegistrationBean(myServlet,"/my","/my02");
        }
    
    
        @Bean
        public FilterRegistrationBean myFilter(){
    
            MyFilter myFilter = new MyFilter();
    //        return new FilterRegistrationBean(myFilter,myServlet());
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
            filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
            return filterRegistrationBean;
        }
    
        @Bean
        public ServletListenerRegistrationBean myListener(){
            MySwervletContextListener mySwervletContextListener = new MySwervletContextListener();
            return new ServletListenerRegistrationBean(mySwervletContextListener);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    二.为什么servlet不会被Filter过滤

    1.DispacherServlet如何注册到bean

    • 给容器中直接放一个DispacherServlet(属性绑定的对应配置文件的配置项为spring.mvc.xx)
    • 后面通过ServletRegistrationBean 把原生的DIspacherServlet注册进来
    • 即绑定解决什么路径(默认映射路径“/”)
    • 以前原生tomcat-servlet多个servlet都能处理到同一层路径,精确优先原则
    • 即我们配置了新的servlet后路径为/my,即如果为my则进入我们配置的serlvet

    三.嵌入式servlet容器

    1.servlet服务器启动

    • springboot应用启动,发现当时是web应用,web场景包中有tomcat
    • web应用创建一个web版IOC容器,此容器的名字为SevletWebServerApplicationContext
    • 该容器会在项目启动过程中搜索SerlvetWebServerFactory (Servletweb服务器工厂----》serlvetweb服务器)
    • springboot默认支持很多web服务器
      • TomcatServletWebServerFactory, JettyServletWebServerFactory, or UndertowServletWebServerFactory
    • 底层直接会有一个自动配置类。ServletWebServerFactoryAutoConfiguration
    • 自动配置类导入了ServletWebServerFactoryConfiguration(配置类)
    • ServletWebServerFactoryConfiguration 配置类 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
    • TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize—this.tomcat.start();
    • 内嵌服务器,就是手动把启动服务器的代码调用(tomcat核心jar包存在)

    2.定制servlet服务器启动

    • 在依赖stater-web中排除tomcat的依赖
    • 加入其他服务器到的场景引入
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-tomcatartifactId>
            exclusion>
        exclusions>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.改动端口字符编码

    • 修改配置文件(推荐)
    • 直接自定义 ConfigurableServletWebServerFactory
    • WebServerFactoryCustomizer
      • 把配置文件的值和ServletWebServerFactory 进行绑定

    四.定制化原理

    1.定制化常见方式

    • 场景starter - xxxxAutoConfiguration - 导入xxx组件 - 绑定xxxProperties – 绑定配置文件项

    2.深入定制化

    • 配置@configuration自定义配置类 来@bean
    • 修改配置文件
    • xxxcustomizer 定制化web容器
    • 用的多 (掌握)
      • 写一个配置类,实现WebMvcConfigure+@Bean扩展组件
      • 在类中定制(重写方法)
    • 使用@EnableWebMvc全面接管mvc(WebMvcConfigure),容器中会执行下面的方法,并且导入WebMvcConfigureSupport类
      • 注意:把所有系统的WebMvcConfigure都拿过来,所有功能的定制都是这些WebMvcConfigure一起生效,只保证springmvc基本的使用
      • 而webmvcConfigurartion里面的配置生效,必须保证容器中没有WebMvcConfigureSupport
  • 相关阅读:
    软考证书还能这样用!拿到证书=获得职称?
    洛谷 P7302 [NOI1998] 免费的馅饼
    Shader入门精要笔记-屏幕后处理效果(1)
    为了摆脱 Android ,亚马逊开发基于Linux的操作系统
    科技碰撞:元宇宙与虚幻引擎,被掩盖的底层逻辑何在?
    19.前端笔记-CSS-显示隐藏元素
    RFID读写器在物联网中的应用与优势
    SQLServe联合主键、联合索引、唯一索引,聚集索引,和非聚集索引、主键、唯一约束和外键约束、索引运算总结
    【简单】 猿人学web第一届 第3题 罗生门
    goland的Markdown拖动插入链接编码有问题
  • 原文地址:https://blog.csdn.net/qq_44724899/article/details/127893491