当spring boot应用被打包为一个fat jar时,是如何访问到web resource的?实际上是通过Archive提供的URL,然后通过Classloader提供的访问classpath resource的能力来实现的。
index.html: 比如需要配置一个index.html,这个可以直接放在代码里的src/main/resources/static目录下。
对于index.html欢迎页,spring boot在初始化时,就会创建一个ViewController来处理:
//ResourceProperties
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/" };
//WebMvcAutoConfigurationAdapter
@Override
public void addViewControllers(ViewControllerRegistry registry) {
Resource page = this.resourceProperties.getWelcomePage();
if (page != null) {
logger.info("Adding welcome page: " + page);
registry.addViewController("/").setViewName("forward:index.html");
}
}
本文介绍了SpringBoot在打包为fatjar时如何访问web资源,主要通过Archive提供的URL和Classloader访问classpathresource。静态资源如index.html通常置于src/main/resources/static目录下。在初始化时,SpringBoot会自动配置ViewController来处理welcome page,如index.html,通过ResourceProperties和WebMvcAutoConfigurationAdapter进行配置。
1497

被折叠的 条评论
为什么被折叠?



