Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying relative path in application.properties in Spring

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

spring.datasource.url=jdbc:hsqldb:file:${project.basedir}/db/init
like image 374
Somasundaram Sekar Avatar asked Apr 29 '16 14:04

Somasundaram Sekar


People also ask

How do you set a relative path?

Relative pathRelative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.

How do I create a path to properties file?

when loading the properties from the file with the method Properties. load(), I need to use the escape character '\' in the path. So my path looks like: C:\\Users\\Harald\\Folder1\\Version1\\Folder2 . And it works this way, no exception is thrown.


2 Answers

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)

I find out that below code works:

upload.dir.location=${user.dir}\\uploadFolder
like image 96
chendu Avatar answered Sep 17 '22 20:09

chendu


@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 NO project.basedir when you're running the application as a jar or war. Outside the local workspace, the source code structure doesn't exist.

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:

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

    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.MyApplicationContextInitializer in your application.properties and Boot will run this class at startup.

Words of caution again:

  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 MyApplicationContextInitializer is in the src/test/java, pwd will be <projectBasedir>/build/classes/test/, not <projectBasedir>/build/classes/main/.

like image 36
Abhijit Sarkar Avatar answered Sep 20 '22 20:09

Abhijit Sarkar