簡體   English   中英

Spring Boot:動態更改靜態資源處理程序?

[英]Spring Boot: Dynamically change the static resource handler?

我的Spring Boot項目在配置中具有以下內容,該項目從本地文件系統提供靜態文件:

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        System.out.println("adding resource handler");
        registry.addResourceHandler("/myfiles/**").addResourceLocations("file:///C:/Users/Pepria/Downloads/static_files/");
    }
}

上面的配置工作正常,但我想在運行時動態更改資源位置。 據我了解,以上代碼在任何邏輯執行之前就運行了。 我該怎么做呢?

您可以使用所需的路徑添加ResourceHandler,如下所示:

registry.addResourceHandler("/myfiles/**").addResourceLocations("file:" + Strings.filePath);

您可以隨時在應用程序中設置Strings.filePath。

public class Strings {
    public static String filePath;
    //or maybe setters getters
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM