0
点赞
收藏
分享

微信扫一扫

【SpringBoot】welcome与favicon功能以及一些底层


欢迎页配置

默认放在static文件夹下,index.html即为欢迎页

手动修改欢迎页的位置

在application.yaml文件中配置如下

spring:
web:
resources:
static-locations: [classpath:/haha/]

【SpringBoot】welcome与favicon功能以及一些底层_ide

 

【SpringBoot】welcome与favicon功能以及一些底层_java_02

 注意:可以配置静态资源路径,但不可以配置静态资源的访问前缀,否则导致index.html不能被默认访问

Favicon

将图标放到静态资源路径,默认是static,因为上面我们改了,所以是haha路径下,图标命名应为favicon.ico

【SpringBoot】welcome与favicon功能以及一些底层_java_03

 

【SpringBoot】welcome与favicon功能以及一些底层_mvc_04

注意:如果浏览器访问网站时网站还没有图标,后配置,但是浏览器没关,以后图标都不会出现,这是因为浏览器session的缘故,所以需要换一个浏览器尝试,同样也不可以配置静态资源路径前缀,否则会影响功能的实现。

底层分析

大多数配置都在这个Servlet文件夹里

【SpringBoot】welcome与favicon功能以及一些底层_ide_05

【SpringBoot】welcome与favicon功能以及一些底层_java_06

 给容器中配了什么

@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {}

WebMvcProperties.class和spring.mvc进行了绑定,WebProperties和spring.web进行了绑定

【SpringBoot】welcome与favicon功能以及一些底层_spring_07

【SpringBoot】welcome与favicon功能以及一些底层_spring boot_08

 配置类只有一个有参构造器

有参构造的所有参数的值都会在构造器中确定

public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
this.resourceProperties = webProperties.getResources();
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
this.mvcProperties.checkConfiguration();
}

分析:



 

 禁用静态资源

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
registration.addResourceLocations(resource);
}
});
}

当add-mappings:false时,所有的静态资源都被禁用 

【SpringBoot】welcome与favicon功能以及一些底层_ide_09

 设置缓存的时间

【SpringBoot】welcome与favicon功能以及一些底层_spring boot_10

 Webjars的规则

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {

registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
registration.addResourceLocations(resource);
}
});
}

搜索webjars的时候,会沿着该路径往下找

【SpringBoot】welcome与favicon功能以及一些底层_spring_11

欢迎页

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
return welcomePageHandlerMapping;
}

这个地方是写死的,所以不能修改静态网页的前缀 

【SpringBoot】welcome与favicon功能以及一些底层_spring boot_12

举报

相关推荐

0 条评论