Reputation: 489
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.
Upvotes: 1
Views: 24036
Reputation: 150
Current directory refers to where we execute our jar. Creating an executable jar via Spring Boot maven plugin and placing application.properties just beside the jar file will work. An example :here
Upvotes: -1
Reputation: 10132
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 from D:\\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" })
where CONF_DIR
is existing environment variable pointing to that directory.
Hope it helps !!
Upvotes: 3
Reputation: 3610
According to ConfigFileApplicationListener:
// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS =
"classpath:/,classpath:/config/,file:./,file:./config/";
file:./
resolve to the working directory where you start the java process.
Upvotes: 6
Reputation: 33091
If you search for "current directory java", you'll end up here with this question. The intention is that if you put an application.properties
in the same directory as the application, it will be picked up by default.
You will not use that feature in development or for test as you shouldn't rely on that feature. But when running your app in production, it might be handy to put environment-specific settings in a configuration file that sits next to the application itself.
Upvotes: 0
Reputation: 4465
I understand that the current directory is the root directory of your project. However, you can change this with -Dspring.config.location=your/config/dir/
.
Have a look at this post enter link description here
Upvotes: 0