首页 > 其他分享 >springboot web开发springmvc自动配置原理

springboot web开发springmvc自动配置原理

时间:2023-10-01 11:39:55浏览次数:42  
标签:web springboot springmvc 配置 class 自动 Configuration public EnableWebMvc


前言

我们也知道springboot启用springmvc基本不用做什么配置可以很方便就使用了但是不了解原理,开发过程中遇到点问题估计就比较头疼,不管了解的深不深入,先巴拉一番再说…
下面我们先看看官网…我的版本是2.3.2版本,发现官网改动也比较大…不同版本自己巴拉下吧,结构虽然变化了,但是内容变的不是很多…
有变动的 都标有: covered later in this document

springmvc 自动配置原理

官网上说明

springboot web开发springmvc自动配置原理_MVC


自动配置在Spring的默认设置之上添加了以下功能

  • ContentNegotiatingViewResolver and BeanNameViewResolver
    这2个bean 配置在WebMvcAutoConfiguration中
  1. 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?))
  2. ContentNegotiatingViewResolver:组合所有的视图解析器的;
  3. 如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;
  4. 代码
@SpringBootApplication
public class ComWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(ComWebApplication.class, args);
    }

    @Bean
    public ViewResolver myViewReolver(){
        return new MyViewResolver();
    }

    public static class MyViewResolver implements ViewResolver{

        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }

}
  • Support for serving static resources, including support for WebJars (see below).静态资源文件夹路径,webjars 在静态资源映射规则中已经讲解
  • 自动注册了 of Converter , GenericConverter , Formatter beans.
    Converter:转换器;
    public String hello(User user):类型转换使用Converter
    Formatter 格式化器; 2017.12.17===Date;
public void addFormatters(FormatterRegistry registry) {
            ApplicationConversionService.addBeans(registry, this.beanFactory);
        }

自己添加的格式化器转换器,我们只需要放在容器中即可

  • HttpMessageConverter:SpringMVC用来转换Http请求和响应的;User—Json;
  • -HttpMessageConverters 是从容器中确定;获取所有的HttpMessageConverter;
    自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)
  • Automatic registration of MessageCodesResolver (see below).定义错误代码生成规则
  • Static index.html support. 静态首页 在静态资源映射规则中已经讲解
  • Custom Favicon support (covered later in this document). favicon.icon 在静态资源映射规则中已经讲解
  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).
    我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)
    初始化WebDataBinder;
    请求数据=====JavaBean;

springboot web开发springmvc自动配置原理_mvc_02

springmvc 扩展

springboot 自动配置远远不能满足我们的开发生产需要所以我们自己需要扩展一些功能
比如我们原来在springmvc的配置文件中写的

<mvc:view‐controller path="/hello" view‐name="success"/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hello"/>
            <bean></bean>
        </mvc:interceptor>
    </mvc:interceptors>

我们都需要进行根据实际需要进行配置

springboot web开发springmvc自动配置原理_mvc_02


If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

如果要保留这些Spring Boot MVC定制并进行更多的MVC定制(拦截器,格式化程序,视图控制器和其他功能),则可以添加自己的类型为WebMvcConfigurer的@Configuration类,但不添加@EnableWebMvc。

@Configuration
public class MySpringMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送hello 跳转到success页面
        registry.addViewController("/hello").setViewName("success");

    }
}

编写一个配置类(@Configuration),是WebMvcConfigurer 类型;不能标注@EnableWebMvc;老版本是WebMvcConfigurerAdapter,这个是抽象类没有都是空方法
既保留了所有的自动配置,也能用我们扩展的配置;

我们一探究竟

原理:

  • WebMvcAutoConfiguration是SpringMVC的自动配置类
  • 、在做其他自动配置时会导入;@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
@Configuration(
        proxyBeanMethods = false
    )
    @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
    @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {

EnableWebMvcConfiguration类

@Configuration(
        proxyBeanMethods = false
    )
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
        private final ResourceProperties resourceProperties;

EnableWebMvcConfiguration父类中

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

    public DelegatingWebMvcConfiguration() {
    }
 //从容器中获取所有的WebMvcConfigurer
    @Autowired(
        required = false
    )
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }

    }

//;将所有的WebMvcConfigurer相关配置都来一起调用;

public void addWebMvcConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.delegates.addAll(configurers);
        }

    }
  • 、容器中所有的WebMvcConfigurer都会一起起作用
  • 、我们的配置类也会被调用;
    效果:SpringMVC的自动配置和我们的扩展配置都会起作用;
    注意这里说的都会起作用是说的针对不同的配置,比如是配置的静态文件映射,有的是viewController等等这些一起起作用

WebMvcRegistrations

如果要提供RequestMappingHandlerMapping,RequestMappingHandlerAdapter或ExceptionHandlerExceptionResolver的自定义实例,并且仍然保留Spring Boot MVC自定义,则可以声明WebMvcRegistrations类型的Bean,并使用它提供这些组件的自定义实例。

完全控制springmvc

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.
如果要完全控制Spring MVC,则可以添加用@EnableWebMvc注释的自己的@Configuration,或者按照@EnableWebMvc的Javadoc中的说明添加自己的@Configuration注释的DelegatingWebMvcConfiguration。

原理:

为什么@EnableWebMvc自动配置就失效了;

  • @EnableWebMvc
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

这样看来是说明我们导入了WebMvcConfigurationSupport

  • WebMvcAutoConfiguration 这个类我们知道是springmvc自动配置的核心类,在没有WebMvcConfigurationSupport这组件的时候才起作用
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {

这么说来我们目前知道WebMvcAutoConfiguration类以及失效了

  • 、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

如何修改SpringBoot的默认配置

模式:
1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;
2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置


标签:web,springboot,springmvc,配置,class,自动,Configuration,public,EnableWebMvc
From: https://blog.51cto.com/u_15935817/7672377

相关文章

  • springboot web开发登录拦截器
    在SpringBoot中我们可以使用HandlerInterceptorAdapter这个适配器来实现自己的拦截器。这样就可以拦截所有的请求并做相应的处理。应用场景日志记录,可以记录请求信息的日志,以便进行信息监控、信息统计等。权限检查:如登陆检测,进入处理器检测是否登陆,如果没有直接返回到登陆页面。性......
  • springmvc 获取项目中的所有请求路径
    springboot/springmvc获取项目中的所有请求路径1.编写业务代码@Autowired privateWebApplicationContextapplicationContext;@GetMapping("/getAllURL") publicRestfulResultgetAllURL(){ //获取springmvc处理器映射器组件对象RequestMappingHandlerMapping无法......
  • SSM(Spring+SpringMVC+MyBatis)框架集成
    引言进行SSM(Spring+SpringMVC+MyBatis)集成的主要原因是为了提高开发效率和代码可维护性。SSM是一套非常流行的JavaWeb开发框架,它集成了Spring框架、SpringMVC框架和MyBatis框架,各自发挥优势,形成了一个完整的开发框架。首先,使用Spring框架可以实现组件的解耦和依赖注入,通过配置......
  • Go每日一库之161:grm(Redis Web管理工具)
    GRM是基于go+vue的web版redis管理工具,部署简单便捷,支持SSH连接,用户校验,操作日志、命令行模式、LUA脚本执行等功能。介绍基于go+vue的web版redis管理工具【Webredismanagementtoolbasedongolangandvue】功能清单管理连接(直连和SSH)、切换DB支持string/lis......
  • SpringBoot框架大晚上报错404--我的路径问题(附上SpringBoot MVC管理系统的简单具体代
    代码application.ymlspring:web:resources:static-locations:classpath:/static/,classpath:/templates/datasource:type:com.alibaba.druid.pool.DruidDataSourceurl:jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf......
  • springboot整合mybatisPlus全技巧(1-整合过程)
    本文基于springboot整合mybatisPlus的各种文章早已烂大街的背景下,根据整合过程,MP开发中的常见技巧,MP开发中遇到的各种坑三个方面,来对这一专题做一个全面且实用的总结,基本上只要你吃透这篇文章,开发中关于mybatisplus你能遇到的问题都能迎刃而解了。整合过程网上对于springboot......
  • WebKit Inside: CSS 样式表解码字符集
    CSS样式表引入有3种方式:外部样式表、内部样式表、行内样式,不同的引入方式,解码样式表的字符集原理不一样。外部样式表外部样式表由link标签引入,当WebKit解析到link标签时就会构造CachedCSSStyleSheet对象。这个对象持有CachedResourceRequest对象和TextResourceDec......
  • CTFer成长记录——CTF之Web专题·[SWPUCTF 2021 新生赛]jicao
    一、题目链接  https://www.nssctf.cn/problem/384二、解法步骤  审计代码:  只需POST传入id=wllmNB,GET传入json=json_encode(array('x'=>'wllm'))即可。  payload:?json={"x":"wllm"},利用hackbar,POST传入id=wllmNB。  拿到flag:三、总结  基本操作。 ......
  • webman:前后端分离上传图片(v1.5.7)
    一,vue:12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838......
  • Java:Springboot和React中枚举值(数据字典)的使用
    目录1、开发中的需求2、实现效果3、后端代码4、前端代码5、接口数据6、完整代码7、参考文章1、开发中的需求开发和使用过程中,通常会涉及四个角色:数据库管理员、后端开发人员、前端开发人员、浏览者数据库使用int类型的数值进行存储(eg:0、1、2)Java代码使用enum枚举类型的对象进行......