24.3 Application property files SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
It mentions current directory twice but this really doesn't mean anything:
I tried putting it in the root of my project (i.e. above src
in the folder that matches the output of java.io.File( "." ).getCanonicalPath()
and System.getProperty("user.dir");
), and I tried putting it with the war files (i.e. in build\libs
)
But the only place to put it that actually works is the default location (src\main\resources
).
So what does "current directory" even mean and where do the files really go?
I need to find the correct external location for the files so I don't have to build database credentials into the app.
The guides say that putting application.properties
in current directory will work and I found the exact current directory to put it in but it still doesn't work, which I can verify by the output of: System.out.println(System.getProperty("spring.datasource.url"));
which is null
It does output the correct value only with an embedded properties file.
I agree with Stephane Nicoll's argument that we generally don't need this for development and test but needed for production where properties file is generally externalized and the one present in source code is not used. This is what works for me ,
java -jar myjar.jar --spring.config.location=file:D:\\RunRC\\application.properties
Directory -
D:\\RunRC
- mentioned in above command is sample from my machine.I keep using properties file of source code i.e. from
\src\main\resources\
in development and test but in production , I comment out entries and if I am starting my jar or war fromD:\\RunRC
then I provide Current Directory as shown in above java command and keep properties file there.Just doing -
@PropertySource({ "application.properties"})
or@PropertySource({ "file:application.properties"})
doesn't pick it up from the directory where jar or war is kept.For database credentials, I would suggest to use OS specific environment variables and use syntax similar to -
@PropertySource({"file:${CONF_DIR}database.properties" })
whereCONF_DIR
is existing environment variable pointing to that directory.Hope it helps !!