简体   繁体   English

Spring Boot:动态更改静态资源处理程序?

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

I have the following in configuration for my Spring Boot project that serves static files from the local filesystem: 我的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/");
    }
}

Above config works fine but I want to change the resource location dynamically at runtime. 上面的配置工作正常,但我想在运行时动态更改资源位置。 As I understand, above code runs before any of my logic executes. 据我了解,以上代码在任何逻辑执行之前就运行了。 How can I go about doing this ? 我该怎么做呢?

You can add a ResourceHandler with your desired path like this: 您可以使用所需的路径添加ResourceHandler,如下所示:

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

You can set Strings.filePath in you application at any time. 您可以随时在应用程序中设置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