7.3.1 SpringBoot的web开发:springboot提供的自动配置
1.自动配置的ViewResolver
1.ContentNegotiatingViewResolver
ContentNegotiatingViewResolver不是自己处理View,而是代理给不同的ViewResolver来处理不同的View
-> 优先级最高
@Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(
beanFactory.getBean(ContentNegotiationManager.class));
// ContentNegotiatingViewResolver uses all the other view resolvers to locate
// a view so it should have a high precedence
resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
return resolver;
}
2.BeanNameViewResolver
在Controller中的一个返回值为字符串(视图名),会根据BeanNameViewResolver去查找Bean(名称为返回字符串)的View来渲染视图
@Bean
@ConditionalOnBean(View.class)
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver() {
BeanNameViewResolver resolver = new BeanNameViewResolver();
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
return resolver;
}
3.InternalResourceViewResolver
是一个极为常用的ViewResolver
-> 通过设置前缀,后缀及控制器中的方法来返回视图名称的字符串,得到实际界面
@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(this.mvcProperties.getView().getPrefix());
resolver.setSuffix(this.mvcProperties.getView().getSuffix());
return resolver;
}
2.自动配置静态资源
在自动配置类中的addResourceHandlers方法中定义了以下 的静态资源的自动配置
1.类路径文件
会把类路径下的文件映射为 -> /** 通过8080/**来访问
"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/“
2.webJar路径
就是把常用的脚本框架封装在jar包下
把/META-INF/resource/webjar/下的静态文件映射为/webjar/**
-> 通过8080/webjar/**访问(见swagger-ui)
3.自动配置Formatter和Converter
如下面的addFormatters,只要定义了Convert,Formatter的接口实现类,就会被注册到Bean中去
4.静态首页的支持
只要报index.html放在
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
classpath:/public/index.html
举例:
1.类路径文件
会把类路径下的文件映射为 -> /** 通过8080/**来访问
"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
2.webJar路径
就是把常用的脚本框架封装在jar包下
把/META-INF/resource/webjar/下的静态文件映射为/webjar/**
-> 通过8080/webjar/**访问(见swagger-ui)
webjar路径
3.自动配置Formatter和Converter
如下面的addFormatters,只要定义了Convert,Formatter的接口实现类,就会被注册到Bean中去
@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);
}
}