天天看點

SpringBoot(5):靜态資源管理,伺服器啟動加載資料

靜态資源管理

SpringBoot有預設的靜态資源處理,我們可以通過WebMvcAutoConfiguration來配置各種屬性。

預設的配置已經足夠讓我們平常使用了,如果有特殊要求則可以通過配置進行修改。

如果想要自己完全控制WebMVC,就需要在@Configuration注解的配置類上增加@EnableWebMvc(@SpringBootApplication 注解的程式入口類已經包含@Configuration),增加該注解以後WebMvcAutoConfiguration中配置就不會生效,你需要自己來配置需要的每一項。這種情況下的配置還是要多看一下WebMvcAutoConfiguration類。

我們既然是快速使用Spring Boot,并不想過多的自己再重新配置。本文還是主要針對Spring Boot的預設處理方式,部配置設定置在application 配置檔案中(.properties 或 .yml)

其中預設配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)

也就是說,如果你有一個資源,放在這幾個檔案夾中的幾個或一個都是可以通路到的。

如果還有别的需求的話就可以自定義,

以增加 /mkdlpres/* 映射到 classpath:/mkdlpres/* 為例的代碼處理為: 

實作類繼承 WebMvcConfigurerAdapter 并重寫方法 addResourceHandlers 

package org.springboot.sample.config;

import org.springboot.sample.interceptor.MyInterceptor1;
import org.springboot.sample.interceptor.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebAppConfigurer 
        extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/mkdlpres/**").addResourceLocations("classpath:/            
mkdlpres/");
           
super.addResourceHandlers(registry); }} 如果我們要指定一個絕對路徑的檔案夾(如 H:/myimgs/ ),則隻需要使用 addResourceLocations 指定即可。
// 可以直接使用addResourceLocations 指定磁盤絕對路徑,同樣可以配置多個位置,注意路徑寫法需要加上file:
registry.addResourceHandler("/myimgs/**").addResourceLocations("file:H:/myimgs/");
           

通過配置檔案配置

上面是使用代碼來定義靜态資源的映射,其實Spring Boot也為我們提供了可以直接在 application.properties(或.yml)中配置的方法。  配置方法如下:
# 預設值為 /**
spring.mvc.static-path-pattern=
# 預設值為 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ 
spring.resources.static-locations=這裡設定要指向的路徑,多個使用英文逗号隔開,
           
使用 spring.mvc.static-path-pattern 可以重新定義pattern,如修改為 /mkdlpres/** ,則通路static 等目錄下的資源檔案應該為 http://localhost:8080/myres/xxx ,修改之前為 http://localhost:8080/xxx  使用 spring.resources.static-locations 可以重新定義 pattern 所指向的路徑,支援 classpath: 和 file: (上面已經做過說明)  注意 spring.mvc.static-path-pattern 隻可以定義一個,目前不支援多個逗号分割的方式。

頁面中使用

上面幾個例子中也已經說明了怎麼通路靜态資源,其實在頁面中使用不管是jsp還是freemarker,并沒有什麼特殊之處,也我們平時開發web項目一樣即可。  下面是我的index.jsp:
<body>
    <img alt="讀取預設配置中的圖檔" src="${pageContext.request.contextPath }/pic.jpg">
    <br/>
    <img alt="讀取自定義配置myres中的圖檔" src="${pageContext.request.contextPath }/myres/fengjing.jpg">
</body>
           
啟動加載資料 當啟動伺服器的時候我們要加載資料的話,在SpringBoot中通過實作接口CommandLineRunner即可。無需别的配置。 Spring Boot應用程式在啟動後,會周遊CommandLineRunner接口的執行個體并運作它們的run方法。也可以利用@Order注解(或者實作Order接口)來規定所有CommandLineRunner執行個體的運作順序。 下面寫兩個類實作CommandLineRunner. MyStartUpRunner1.java
package com.springboot.study.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=1)
public class MyStartUpRunner1 implements CommandLineRunner{

	@Override
	public void run(String... arg0) throws Exception {
		System.out.println("================>伺服器啟動,加載資料!(1)");
	}

}
           
MyStartUpRunner2.java
package com.springboot.study.runner;


import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


@Component
@Order(value=2)
public class MyStartUpRunner2 implements CommandLineRunner{


	@Override
	public void run(String... arg0) throws Exception {
		System.out.println("================>伺服器啟動,加載資料!(2)");
	}


}
           
然後運作程式,就可以看到運作的順序了。
================>伺服器啟動,加載資料!(1)
================>伺服器啟動,加載資料!(2)