1 2 3 4 5 |
<code class="prism language-java has-numbering"><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> CLASSPATH_RESOURCE_LOCATIONS <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"classpath:/META-INF/resources/"</span><span class="token punctuation">,</span> <span class="token string">"classpath:/resources/"</span><span class="token punctuation">,</span> <span class="token string">"classpath:/static/"</span><span class="token punctuation">,</span> <span class="token string">"classpath:/public/"</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> |
可以看到springboot默认加载资源的路径是在calsspath下,这样jar打包的web项目资源文件只能放在jar内。这样修改和维护起来非常不方便。
参考官方文档,static资源也可以放在jar包外部,只要在配置文件中增加static-locations: 就可以直接在配置文件中覆盖默认的静态资源路径的配置信息
1 2 3 4 5 6 7 8 |
<span class="token key atrule">spring</span><span class="token punctuation">:</span> <span class="token key atrule">resources</span><span class="token punctuation">:</span> <span class="token comment">#web静态页面路径</span> <span class="token comment"># static-locations: file:///E:/workspace/demo/target/demo/static/,file:///E:/workspace/demo/target/demo/public/</span> <span class="token comment">#当前启动目录下相对路径</span> <span class="token key atrule">static-locations</span><span class="token punctuation">:</span> file<span class="token punctuation">:</span>./static/<span class="token punctuation">,</span>file<span class="token punctuation">:</span>./public/ |