SpringBoot___web开发自动配置

Web开发的自动配置类为:org.springframework.boot.autoconfigure
1. web自动配置解析
 SpringBoot项目的web开发的自动配置位置如下:
父级位置
然后寻找
子级位置

2. 自动配置的ViewResolver(视图解析器)
 我们都知道在使用Spring的过程中都离不开解析器,而最后返回的视图是根据视图解析器来操作的。而SpringBoot的web开发的自动配置中,肯定会有一个默认的视图解析器。我们来看下源码:

@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver(){
    InternalResourceResolverViewResolver resolver = 
                            new InternalResourceViewResolver();
    resolver.setPrefix(this.mvcProperties.getView().getPrefix());
    resolver.setPrefix(this.mvcProperties.getView().getSuffix());
    return resolver;
}

@Bean
@ConditionalOnBean(View.class)
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver(){
    BeanNameViewResolver resolver = new BeanNameViewResolver();
    resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
    return resolver;
}

3. 自动配置的View(视图)
上面代码中默认的视图解析器ViewResolver会返回一个视图View。我们来看下这个视图的定义,其位置在org.springframework.boot.autoconfigure.web.WebMvcProperties.View
 其代码如下:

public static class View{
    //视图前缀
    private String prefix;
    //视图后缀
    private String suffix;

    public String getPrefix() {
    return this.prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return this.suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

View可以在我们全局配置中进行指定。

4. 静态资源配置
 对于静态资源我们可以进行配置其位置,在这边建议将静态资源防止在某个特定的文件下。
 将静态资源放置在webapp下的static目录中,spring.resources.static-locations = XXX。当我们设置进入规则为*.XXX不设置,即可通过地址来访问。

猜你喜欢

转载自blog.csdn.net/pseudonym_/article/details/80625138
今日推荐