最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中對(duì)SpringMVC的自動(dòng)配置詳解

 更新時(shí)間:2023年10月27日 09:35:59   作者:夜聆離殤  
這篇文章主要介紹了SpringBoot中的SpringMVC自動(dòng)配置詳解,Spring MVC自動(dòng)配置是Spring Boot提供的一種特性,它可以自動(dòng)配置Spring MVC的相關(guān)組件,簡(jiǎn)化了開(kāi)發(fā)人員的配置工作,需要的朋友可以參考下

一. SpringMVC自動(dòng)配置

spring boot自動(dòng)配置好了springmvc,以下是springboot對(duì)springmvc的自動(dòng)配置(WebMvcAutoConfiguration.class)

1. Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

1) 自動(dòng)配置了ViewResolver(視圖解析器:根據(jù)方法的返回值得到視圖對(duì)象(View),視圖對(duì)象決定如何渲染(轉(zhuǎn)發(fā)?重定向?))

2) ContentNegotiatingViewResolver:組合所有的視圖解析器的;

public View resolveViewName(String viewName, Locale locale) throws Exception {
        RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
        Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
        List<MediaType> requestedMediaTypes = getMediaTypes(((ServletRequestAttributes) attrs).getRequest());
        if (requestedMediaTypes != null) {
            List<View> candidateViews = getCandidateViews(viewName, locale, requestedMediaTypes);          //獲取所有View
            View bestView = getBestView(candidateViews, requestedMediaTypes, attrs);                  //獲得正確的View
            if (bestView != null) {
                return bestView;
            }
        }

3) 如何定制:我們可以自己給容器中添加一個(gè)視圖解析器;自動(dòng)的將其組合進(jìn)來(lái);

2. Support for serving static resources, including support for WebJars (see below):靜態(tài)資源文件夾路徑,webjars

3. Static index.html support.:靜態(tài)首頁(yè)訪問(wèn)

4. Custom Favicon support (see below).:favicon.ico

5. 自動(dòng)注冊(cè)了 of Converter , GenericConverter , Formatter beans.

1)Converter:轉(zhuǎn)換器;例如: public String hello(User user):頁(yè)面上類型轉(zhuǎn)換使用Converter(表單屬性能對(duì)應(yīng)上User屬性)

2)Formatter:格式化器;例如:日期格式化

@Override
        public void addFormatters(FormatterRegistry registry) {
            for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
                registry.addConverter(converter);
            }
            for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
                registry.addConverter(converter);
            }
            for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
                registry.addFormatter(formatter);
            }
        }

3)自己添加的格式化類型轉(zhuǎn)化器,我們只需要放在容器中即可

6. Support for HttpMessageConverters (see below).

1)HttpMessageConverter:SpringMVC用來(lái)轉(zhuǎn)換Http請(qǐng)求和響應(yīng)的;User---Json;

2)HttpMessageConverters 是從容器中確定;獲取所有的HttpMessageConverter;自己給容器中添加HttpMessageConverter,只需要將自己的組件注冊(cè)容器中(@Bean,@Component)

7. Automatic registration of MessageCodesResolver (see below).定義錯(cuò)誤代碼生成規(guī)則

8. Automatic use of a ConfigurableWebBindingInitializer bean (see below).

我們可以配置一個(gè)ConfigurableWebBindingInitializer來(lái)替換默認(rèn)的;(添加到容器)

@Override
        protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
            try {
                return this.beanFactory.getBean(ConfigurableWebBindingInitializer.class);
            }
            catch (NoSuchBeanDefinitionException ex) {
                return super.getConfigurableWebBindingInitializer();
            }
        }

二. 擴(kuò)展SpringMVC

例如原先SpringMVC的配置文件中我們可以配置攔截器

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean></bean>
    </mvc:interceptor>
</mvc:interceptors>

在SpringBoot中我們是全注解式開(kāi)發(fā),因此我們?nèi)绻?;想?shí)現(xiàn)攔截器的功能,則需要編寫一個(gè)配置類@Configuration,是WebMvcConfigurerAdapter類型;不能標(biāo)注@EnableWebMvc;

既保留了所有的自動(dòng)配置,也能用我們擴(kuò)展的配置;

//使用WebMvcConfigurerAdapter可以來(lái)擴(kuò)展SpringMVC的功能@Configurationpublic class MyMvcConfig extends WebMvcConfigurationSupport{
//使用WebMvcConfigurerAdapter可以來(lái)擴(kuò)展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{
  @Autowired
  private UserTokenInterceptor userTokenInterceptor;
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    // 多個(gè)攔截器組成一個(gè)攔截器鏈
    // addPathPatterns 用于添加攔截規(guī)則
    // excludePathPatterns 用戶排除攔截
    registry.addInterceptor(userTokenInterceptor).addPathPatterns("/**")
      .excludePathPatterns("/account/login","/account/register");
    super.addInterceptors(registry);
  }

}

原理:

1)WebMvcAutoConfiguration是SpringMVC的自動(dòng)配置類

2)在做其他自動(dòng)配置時(shí)會(huì)導(dǎo)入;@Import(EnableWebMvcConfiguration.class)

@Autowired(required = false)
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }
    }

3)容器中所有的WebMvcConfigurer都會(huì)一起起作用;

4)我們的配置類也會(huì)被調(diào)用;效果:SpringMVC的自動(dòng)配置和我們的擴(kuò)展配置都會(huì)起作用;

三. 全面接管SpringMVC

SpringBoot對(duì)SpringMVC的自動(dòng)配置不需要了,所有都是我們自己配置;所有的SpringMVC的自動(dòng)配置都失效了

我們需要在配置類中添加@EnableWebMvc即可;

//使用WebMvcConfigurerAdapter可以來(lái)擴(kuò)展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    // super.addViewControllers(registry);
    //瀏覽器發(fā)送 /hello 請(qǐng)求來(lái)到 success
    registry.addViewController("/hello").setViewName("success");
  }
}

原理:

1)@EnableWebMvc的核心 

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

2)

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
}

3)

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
//容器中沒(méi)有這個(gè)組件的時(shí)候,這個(gè)自動(dòng)配置類才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
        ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4)@EnableWebMvc將WebMvcConfigurationSupport組件導(dǎo)入進(jìn)來(lái);

5)導(dǎo)入的WebMvcConfigurationSupport只是SpringMVC最基本的功能; 

四. 如何修改SpringBoot的默認(rèn)配置

1)、SpringBoot在自動(dòng)配置很多組件的時(shí)候,先看容器中有沒(méi)有用戶自己配置的(@Bean、@Component)如果有就用用戶配置的,如果沒(méi)有,才自動(dòng)配置;

如果有些組件可以有多個(gè)(ViewResolver)將用戶配置的和自己默認(rèn)的組合起來(lái);

2)、在SpringBoot中會(huì)有非常多的xxxConfigurer幫助我們進(jìn)行擴(kuò)展配置3)、在SpringBoot中會(huì)有很多的xxxCustomizer幫助我們進(jìn)行定制配置

到此這篇關(guān)于SpringBoot中對(duì)SpringMVC的自動(dòng)配置詳解的文章就介紹到這了,更多相關(guān)SpringMVC自動(dòng)配置詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java中List對(duì)象排序通用方法

    java中List對(duì)象排序通用方法

    這篇文章主要介紹了java中List對(duì)象排序通用方法,涉及java針對(duì)List對(duì)象的操作技巧,需要的朋友可以參考下
    2015-05-05
  • LinkList的底層數(shù)據(jù)結(jié)構(gòu)及優(yōu)缺點(diǎn)詳解

    LinkList的底層數(shù)據(jù)結(jié)構(gòu)及優(yōu)缺點(diǎn)詳解

    鏈表由節(jié)點(diǎn)組成,每個(gè)節(jié)點(diǎn)包含數(shù)據(jù)域和指針域,節(jié)點(diǎn)在內(nèi)存中非連續(xù)分布,通過(guò)指針鏈接形成邏輯上的線性關(guān)系,鏈表支持動(dòng)態(tài)大小、高效插入/刪除,但隨機(jī)訪問(wèn)效率低,且需要手動(dòng)管理指針
    2025-11-11
  • 教你如何在IDEA?中添加?Maven?項(xiàng)目的?Archetype(解決添加不起作用的問(wèn)題)

    教你如何在IDEA?中添加?Maven?項(xiàng)目的?Archetype(解決添加不起作用的問(wèn)題)

    這篇文章主要介紹了如何在?IDEA?中添加?Maven?項(xiàng)目的?Archetype(解決添加不起作用的問(wèn)題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • IDEA 熱部署設(shè)置(JRebel插件激活)

    IDEA 熱部署設(shè)置(JRebel插件激活)

    這篇文章主要介紹了IDEA 熱部署設(shè)置(JRebel插件激活),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java 如何獲取url地址文件流

    Java 如何獲取url地址文件流

    這篇文章主要介紹了Java 如何獲取url地址文件流,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • SpringBoot集成iText實(shí)現(xiàn)電子簽章功能

    SpringBoot集成iText實(shí)現(xiàn)電子簽章功能

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何集成iText實(shí)現(xiàn)電子簽章功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-10-10
  • 詳解Spring Boot Junit單元測(cè)試

    詳解Spring Boot Junit單元測(cè)試

    本篇文章主要介紹了詳解Spring Boot Junit單元測(cè)試,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • spring?boot教程之IDEA環(huán)境下的熱加載與熱部署

    spring?boot教程之IDEA環(huán)境下的熱加載與熱部署

    這篇文章主要介紹了spring?boot系列教程中的IDEA環(huán)境下的熱加載與熱部署的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • poi導(dǎo)出word表格的操作講解

    poi導(dǎo)出word表格的操作講解

    這篇文章主要介紹了poi導(dǎo)出word表格的操作講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • java后臺(tái)接受到圖片后保存方法

    java后臺(tái)接受到圖片后保存方法

    在本篇文章里小編給大家整理了關(guān)于java后臺(tái)接受到圖片后怎么保存的相關(guān)知識(shí)點(diǎn),需要的朋友們參考學(xué)習(xí)下。
    2019-06-06

最新評(píng)論

SHOW| 迁安市| 甘肃省| 白河县| 观塘区| 澳门| 荣成市| 和静县| 元江| 丰原市| 吕梁市| 宾阳县| 顺昌县| 康定县| 吕梁市| 隆化县| 岳池县| 鄂托克旗| 兴和县| 金川县| 开远市| 象山县| 泾源县| 碌曲县| 晋城| 怀来县| 宁波市| 宁化县| 常德市| 水富县| 和林格尔县| 桃江县| 磐安县| 诸暨市| 西城区| 秀山| 夹江县| 台北市| 香河县| 涿州市| 株洲市|