如何在Spring Boot应用程序中动态添加资源处理程序?

时间:2019-06-10 07:10:10

标签: java spring-boot resources

我有这段代码:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/images/**")
            .addResourceLocations("file:static/dragontail-9.11.1/9.11.1/img/")
            .resourceChain(true);
}

只要有可以下载的更新,我都希望在运行时更新数据文件。因此,我需要能够支持在运行时更改该版本号。我可以为此使用一个变量,但是问题是我需要一次支持多个版本

我有一个HashSet,用于管理当前支持的版本号。我该如何从它动态地匹配资源处理程序呢?

我也尝试过在.addResourceLocations中使用蚂蚁匹配器,这会有所帮助,但是似乎仅在.addResourceHandler中支持蚂蚁匹配器


我相信我已经解决了这个问题-直接处理路径匹配应该可以让我在运行时更改资源路径。我们可以扩展AntPathMatcher#match来将资源任意匹配到URI。我实际上还没有尝试过,但是这样做似乎没有问题。

@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/images/**")
            .addResourceLocations("file:static/dragontail-9.11.1/img/")
            .resourceChain(true);
}

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    AntPathMatcher matcher = new AntPathMatcher();
    matcher.setCaseSensitive(false);
    configurer.setPathMatcher(matcher);
}
}

0 个答案:

没有答案