简体   繁体   中英

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:

@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:

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

You can set Strings.filePath in you application at any time.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM