spring Spring中application.properties中指定相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36940458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:04:45  来源:igfitidea点击:

Specifying relative path in application.properties in Spring

springspring-bootspring-properties

提问by Somasundaram Sekar

Is there a way we can lookup file resources using relative path in application.properties file in Spring boot application as specified below

有没有一种方法可以使用如下指定的 Spring boot 应用程序中 application.properties 文件中的相对路径查找文件资源

spring.datasource.url=jdbc:hsqldb:file:${project.basedir}/db/init

采纳答案by Abhijit Sarkar

@membersound answer is just breaking up the hardcoded path in 2 parts, not dynamically resolving the property. I can tell you how to achieve what you're looking for, but you need to understand is that there is NOproject.basedirwhen you're running the application as a jar or war. Outside the local workspace, the source code structure doesn't exist.

@membersound 答案只是将硬编码路径分为两部分,而不是动态解析属性。我可以告诉你如何实现你要找的东西,但你要明白的是,有NOproject.basedir,当你运行的应用程序作为一个罐子或War。在本地工作区之外,源代码结构不存在。

If you still want to do this for testing, that's feasible and what you need is to manipulate the PropertySources. Your simplest option is as follows:

如果您仍然想这样做以进行测试,那是可行的,您需要的是操作PropertySources。您最简单的选择如下:

Define an ApplicationContextInitializer, and set the property there. Something like the following:

定义一个ApplicationContextInitializer, 并在那里设置属性。类似于以下内容:

    public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext appCtx) {
        try {
            // should be /<path-to-projectBasedir>/build/classes/main/
            File pwd = new File(getClass().getResource("/").toURI());
            String projectDir = pwd.getParentFile().getParentFile().getParent();
            String conf = new File(projectDir, "db/init").getAbsolutePath();
            Map<String, Object> props = new HashMap<>();
            props.put("spring.datasource.url", conf);
            MapPropertySource mapPropertySource = new MapPropertySource("db-props", props);
            appCtx.getEnvironment().getPropertySources().addFirst(mapPropertySource);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }}

Looks like you're using Boot, so you can just declare context.initializer.classes=com.example.MyApplicationContextInitializerin your application.propertiesand Boot will run this class at startup.

看起来你正在使用 Boot,所以你可以context.initializer.classes=com.example.MyApplicationContextInitializer在你的中声明application.properties,Boot 将在启动时运行这个类。

Words of cautionagain:

再次警告

  1. This will not work outside the local workspace as it depends on the source code structure.

  2. I've assumed a Gradle project structure here /build/classes/main. If necessary, adjust according to your build tool.

  3. If MyApplicationContextInitializeris in the src/test/java, pwdwill be <projectBasedir>/build/classes/test/, not <projectBasedir>/build/classes/main/.

  1. 这在本地工作区之外不起作用,因为它取决于源代码结构。

  2. 我在这里假设了一个 Gradle 项目结构/build/classes/main。如有必要,请根据您的构建工具进行调整。

  3. 如果MyApplicationContextInitializer是在src/test/javapwd将会是<projectBasedir>/build/classes/test/,不是<projectBasedir>/build/classes/main/

回答by chendu

I'm using spring boot to build a upload sample, and meet the same problem, I only want to get the project root path. (e.g. /sring-boot-upload)

我在用spring boot构建一个上传示例,遇到同样的问题,我只想获取项目根路径。(例如 /sring-boot-upload)

I find out that below code works:

我发现下面的代码有效:

upload.dir.location=${user.dir}\uploadFolder

回答by membersound

your.basedir=${project.basedir}/db/init
spring.datasource.url=jdbc:hsqldb:file:${your.basedir}

@Value("${your.basedir}")
private String file;

new ClassPathResource(file).getURI().toString()