SpringBoot笔记(5)

SpringBoot笔记(5)

一、Web原生组件注入

1、使用Servlet API

@ServletComponentScan(basePackages = “com.atguigu.admin”) :指定原生Servlet组件都放在那里

@WebServlet(urlPatterns = “/my”):效果:直接响应,没有经过Spring的拦截器?

@WebFilter(urlPatterns={“/css/*”,“/images/*”})

@WebListener

推荐可以这种方式;

扩展:DispatchServlet 如何注册进来

  • 容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc。
  • 通过 ServletRegistrationBean

    把 DispatcherServlet 配置进来。
  • 默认映射的是 / 路径。

<span>SpringBoot笔记(5)</span>

Tomcat-Servlet;

多个Servlet都能处理到同一层路径,精确优选原则

A: /my/

B: /my/1

2、使用RegistrationBean

ServletRegistrationBean`, `FilterRegistrationBean`, and `ServletListenerRegistrationBean
@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、定制化的常见方式

  • 修改配置文件;
  • xxxxxCustomizer;
  • 编写自定义的配置类 xxxConfiguration;+ @Bean替换、增加容器中默认组件;视图解析器
  • Web应用 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能;+ @Bean给容器中再扩展一些组件
@Configuration
public class AdminWebConfig implements WebMvcConfigurer
  • @EnableWebMvc + WebMvcConfigurer —— @Bean 可以全面接管SpringMVC,所有规则全部自己重新配置; 实现定制和扩展功能

    • 原理
    • 1、WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类。静态资源、欢迎页…..
    • 2、一旦使用 @EnableWebMvc 、。会 @Import(DelegatingWebMvcConfiguration.class)
    • 3、DelegatingWebMvcConfiguration 的 作用,只保证SpringMVC最基本的使用
      • 把所有系统中的 WebMvcConfigurer 拿过来。所有功能的定制都是这些 WebMvcConfigurer 合起来一起生效
      • 自动配置了一些非常底层的组件。RequestMappingHandlerMapping、这些组件依赖的组件都是从容器中获取
      • public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport
    • 4、WebMvcAutoConfiguration 里面的配置要能生效 必须 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    • 5、@EnableWebMvc 导致了 WebMvcAutoConfiguration 没有生效。
  • … …

2、原理分析套路

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115187.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android DeepLink介绍与使用

    Android DeepLink介绍与使用前段时间公司让调研一下DeepLink,说以后会用到,之前看了很久,并做了个demo,现整理一下,方便以后查阅,如果有幸帮助到其他人就更好了。基本概念DeepLink,又叫deeplinking,中文翻译作深层链接。简单地从用户体验来讲,DeepLink,就是可以让你在手机的浏览器/GoogleSearch上点击搜索的结果,便能直接跳转到已安装的应用中的某一个页面的技术。对于不…

    2022年6月18日
    109
  • ECharts介绍及使用方法

    ECharts介绍及使用方法前面做项目时用到ECharts,今天特此整理一下,作为笔记,同时希望帮助更多人。首先简单介绍一下,ECharts是一个纯JavaScript图表库,底层依赖于轻量级的Canvas类库ZRender,基于BSD开原协议,是一款非常优秀的可视化前端框架。官网地址:http://echarts.baidu.com/1.首先在官网选择合适的下载版本http://echart…

    2022年6月12日
    42
  • Spring Cloud 与 Docker 实战[通俗易懂]

    Spring Cloud 与 Docker 实战[通俗易懂]Spring Cloud 与 Docker 实战

    2022年4月23日
    59
  • linux find命令的使用_linux打包命令tar

    linux find命令的使用_linux打包命令tarLinux中find命令是系统中查找文件的命令,可以帮助用户快速找出自己所需要的文件。通过文件名查找find-name文件名find/etc/-namepasswd###查找/etc目录下的passwd通过文件嵌套层数查找find-maxdepth层数###查找文件时最大层数find-mindepth层数###查找文件时最小层数12例;我们查找/etc中所有.conf文件,可以看…

    2022年10月9日
    2
  • Apache kylin 入门

    Apache kylin 入门

    2021年11月27日
    43
  • pycharm下载和安装教程_pycharm下载官网

    pycharm下载和安装教程_pycharm下载官网python,pycharm下载安装使用

    2022年8月27日
    22

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号