Spring Boot Web相關配置

經過WebMvcAutoConfiguration發現Spring Boot作了許多自動配置。html

  1.自動配置的VeiwResolverjava

  1. ContentNegotiatingViewResolver:自身不處理view,而是代理給不一樣的ViewResovler來處理不一樣的view,優先級最高。
    public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
            ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
            resolver.setContentNegotiationManager((ContentNegotiationManager)beanFactory.getBean(ContentNegotiationManager.class));
            resolver.setOrder(-2147483648);
            return resolver;
        }

     

  2. BeanNameViewResolver:根據controller方法中返回的字符串的view來渲染視圖。
    @Bean
    @ConditionalOnBean({View.class})
    @ConditionalOnMissingBean
    public BeanNameViewResolver beanNameViewResolver() {
         BeanNameViewResolver resolver = new BeanNameViewResolver();
         resolver.setOrder(2147483637);
         return resolver;
    }

     

  3. InternalResourceViewResolver:主要經過設置前綴、後綴以及控制器方法中返回的視圖名字符串獲得實際頁面。
    @Bean
    @ConditionalOnMissingBean
    public InternalResourceViewResolver defaultViewResolver() {
         InternalResourceViewResolver resolver = new InternalResourceViewResolver();
         resolver.setPrefix(this.mvcProperties.getView().getPrefix());
         esolver.setSuffix(this.mvcProperties.getView().getSuffix());
         return resolver;
    }

     

  2.自動配置的靜態資源web

在自動配置類中addResourceHandlers方法中定義瞭如下靜態資源配置,可繼承起抽象父類,覆寫該方法進行擴展。spring

public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(!this.resourceProperties.isAddMappings()) {
            logger.debug("Default resource handling disabled");
        } else {
            Integer cachePeriod = this.resourceProperties.getCachePeriod();
            if(!registry.hasMappingForPattern("/webjars/**")) {
                this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
            }

            String staticPathPattern = this.mvcProperties.getStaticPathPattern();
            if(!registry.hasMappingForPattern(staticPathPattern)) {
                this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
            }

        }
    }
  1. 類路徑文件
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

    把類路徑下的/static、/public、/resources和/META-INF/resources文件夾下的靜態文件直接映射爲/**。mvc

  2. webjar app

         將經常使用的腳本框架封裝在jar包中,把webjar的/META-INF/resources/webjars/下的靜態文件映射爲/webjar/**,能夠經過http://ip:port/webjar/**訪問。框架

  3.自動配置的Formatter和Converterthis

public void addFormatters(FormatterRegistry registry) {
        Iterator var2 = this.getBeansOfType(Converter.class).iterator();

        while(var2.hasNext()) {
            Converter formatter = (Converter)var2.next();
            registry.addConverter(formatter);
        }

        var2 = this.getBeansOfType(GenericConverter.class).iterator();

        while(var2.hasNext()) {
            GenericConverter formatter1 = (GenericConverter)var2.next();
            registry.addConverter(formatter1);
        }

        var2 = this.getBeansOfType(org.springframework.format.Formatter.class).iterator();

        while(var2.hasNext()) {
            org.springframework.format.Formatter formatter2 = (org.springframework.format.Formatter)var2.next();
            registry.addFormatter(formatter2);
        }

    }

只要咱們程序中自定義了Converter、GenericConverter和Formatter接口的實現類Bean,這些Bean就會自動註冊到Spring MVC中。spa

  4.自動配置的HttpMessageConvertersdebug

    WebMvcAutoConfiguration中,註冊了messageConverters

@Autowired
private HttpMessageConverters messageConverters;

public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.addAll(this.messageConverters.getConverters());
}

若是要新增自定義的HttpMessageConverter,則只要定義一個本身的HttpMessageConverters的Bean,在Bean中註冊 定義好的HttpMessageConverter便可。

@Bean
public HttpMessageConverters customConverters(){
    HttpMessageConverter customConverter1 = new CustomConverter();
    HttpMessageConverter customConverter2 = new CustomConverter();
    return new HttpMessageConverters(customConverter1 ,customConverter2);
}

  5.靜態首頁的支持

classpath:/META-INF/resources/index.html

classpath:/resources/index.html

classpath:/static/index.html

classpath:/public/index.html

相關文章
相關標籤/搜索