标题翻译
Resource folder not found when running as a jar while external folder can be found
问题
我在Spring Boot 1.5.1 RELEASE中遇到了这个问题。
在我的属性中,我有一个属性config-dir。使用资源处理程序,我可以通过以下方式添加我的配置文件夹:
public PortalAppConfig(@Value("${config-dir}") String configDir) {
this.configDir = configDir;
}
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/assets/config/**")
.addResourceLocations(String.format("file:%s", configDir));
}
当我将config-dir声明为外部文件夹,例如config-dir: ${user.home}/externalConfigFolder/
时,一切正常工作。
然而,我不知道如何指向JAR文件中的目录,以便可以使用JAR文件中的目录。我尝试过classpath:static/assets/config
,static/assets/config
以及许多其他变体。
以下图片显示了资源和目标的目录树:
英文翻译
I have this problem in spring boot 1.5.1RELEASE
In my properties i have got a property config-dir. Using resource handler I can add my config folder by:
public PortalAppConfig(@Value("${config-dir}") String configDir) {
this.configDir = configDir;
}
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/assets/config/**")
.addResourceLocations(String.format("file:%s", configDir));
}
When I declare config-dir as an external folder, like config-dir: ${user.home}/externalConfigFolder/
everything works fine.
However I have no idea how to point path to that directory, so It could use directory from JAR file. I tried
classpath:static/assets/config
, static/assets/config
and many other variations.
Those pictures shows directory tree for both resources and target
I know that the question sound similar to https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar
but in my case I do not want to 'use' a file inside a jar, but point to the whole directory (using path as a String) and add it to resourceHandler.
I am building project using maven. Java 8.
专注分享java语言的经验与见解,让所有开发者获益!
评论