1、使用SpringBoot
使用SpringBoot:
- 1)创建SpringBoot应用,选中我们需要的模块;
- 2)SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量的配置就可以运行起来。
- 3)自己填写业务代码
最重要的一点,我们需要了解自动配置的原理。
在引用的时候,我们要知道SpringBoot帮我们配置了什么,能不能修改,能修改哪些,能不能扩展,。。。
总结一下就是:
XXXAutoConfiguration :帮我们给容器中自动配置组件
XXXProperties:配置类来封装配置文件的内容
2、SpringBoot对静态资源的管理
我们先看看WEBMVCAutoConfiguration的内容可以看到
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
- 1)在这个/webjars/**里,都去classpath:/META-INF/resources/webjars/找资源
- webjars以jar包的方式引入静态资源
- 我们登陆官网webjars官网
我们在这里引入bootstrap实验一下
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.4.0</version>
</dependency>
刷新一下maven就刻意看到引入了
- 2)我们在看看ResourceProperties,
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private String[] staticLocations;
private boolean addMappings;
private final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
可以看到规定的几个静态资源的文件夹
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
除了规定了静态路径。还可以设置缓存等消息。
- 3)我们还看到了静态页面的欢迎页的设置
/*欢迎页的映射*/
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors());
return welcomePageHandlerMapping;
}
private Optional<Resource> getWelcomePage() {
String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
指向了location + "index.html"