I have a number of Java property files which used to be included in the standard resources directory and the following property holder of Spring works fine.
<context:property-placeholder location="classpath:/project/myproperty.properties/>
For some reason, I need to move them to another location so that they are not included as part of the JAR. The deployment script will handle them and copy them to a specific location called config.
I added the new location to the classpath argument of my java command, however, the above statement always fails, complaining no property file is found. My command is a follows
java -classpath "C:\Deployment\config" ............
And the property file does exist under:
C:\Deployment\config\project\myproperty.properties
Can someone let me know what I did wrong?
You can try to do it like this:
<context:property-placeholder location="file:${my.config.location}\project\myproperty.properties"/>
And to add the property in the environment properties:
java -Dmy.config.location="C:\Deployment\config" ...
Hope this helps.
Related
I am new to Springboot and I am having trouble with externalizing my properties files.
I have multiple ".properties" files that I have kept in a subdirectory "config/". I have removed context-placeholders from my project and have included the comma-separated properties files while executing the jar.
eg. java -jar myjar.jar --spring.config.location=file:////config/PROP1.properties, file:///config/PROP2.properties -debug
I have few questions
Why are the files in the config directory not being read even after explicitly mentioning where to look?
I have my own dependencies in the project that have same-named properties files packed in its jar. Is that creating any sort of problem when SpringBoot tries to read the files from the config folder while executing my project jar?
Update
Now I am keeping only a single properties file suppose ABC.properties outside the jar in the same directory . I am using the name "ABC" instead of "application". I am using the below command
java -Dserver.log.dir=/path/to/log/dir -jar myjar.jar --server.port=9090 --spring.config.name=ABC --prop1=val1
I have overriden a property in my external property file but I don't see the overriden value being used when I run the application. My new questions are
Is there anything wrong with the command-line?
I have the following line in xml bean configuration
<context:property-placeholder location="ABC.properties" />. Is this causing any sort of problems to detect and use the external properties?
If the above is true and I have to remove the line how will SpringBoot understand from where the property values are to be imported?
(Not related directly to the question) Is there a order that I need to follow while giving command line arguments?
Spring boot has explicit indicate how to write this external configuration. See doc ref here: https://docs.spring.io/spring-boot/docs/2.5.2/reference/htmlsingle/#features.external-config.files
Basically, you need to specify the location like this:
--spring.config.location=classpath:/somefolder/somefile.properties
--spring.config.location=file:./somefolder/somefile.properties
From your command line, it seems that you are missing one dash, and also using a wrong format of file schema.
Q1: Why are the files in the config directory not being read even after explicitly mentioning where to look?
A1: If your config folder is beside your jar file, your command should be like
java -jar myjar.jar --spring.config.location=file:./config/PROP1.properties
meanwhile, you can also use the full path to target your config file.
Q2: If I don't mention the properties files names explicitly as an argument then they won't be picked up even when they are in the config directory
A2: No. See this doc, Spring Boot will try to find config files from the four places:
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
If spring.config.location is not set, files in these folders named application.properties will be treated as valid config file. You can change the accepted file name by setting property spring.config.name.
Q3 About Config File Priority
A3 As described in the doc mentioned earlier, if same name properties appear in different config files, locations higher in the list override lower items.
Another tip, it will be better to remove config files inside the jar file, so you can get a full view of configuration just in one place (the externalized config folder);
im trying to access a property in a .conf file located in the target directory with my jar and its returning null. i followed the spring documentation here: https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-script-customization-conf-file
i believe i should be able to access the properties of the app.conf by simply coding the following:
System.out.println(System.getProperty("dbName"));
System.out.println(System.getProperty("dbUser"));
System.out.println(System.getProperty("dbPassword"));
all three return nulls when i try to run the jar from the terminal. my app.conf file looks like so:
JAVA_OPTS=-DdbName=RandomDb -DdbUser=devUser -DdbPassword=password90210
any recommendations to fix this? i know i can add them to the vm args in the run configs buts i would like to get this to work from a .conf file to avoid exposing sensitive information.
I try to load webapp project's settings from own config.properties file, but there's no success: the file not found. I can't use ServletContext method, because i've access the file from ordinary class.
file = Config.class.getClassLoader().getResourceAsStream("/config.properties");
- it's returns null.
I've tried to put the file to WEB-INF and resources folders, still doesn't work :(
Any ideas how to force this work?
Try putting files in WEB-INF/classes .
If you put your config in src, than after compile it will be in WEB-INF/classes.
My recommendation for configuration files that would solve your problem is to place them in a folder that depends on an environment variable.
If you application is called MyApp, then you can force the user to place that file in the folder that points the environment variable MY_APP. You could in fact make it mandatory to run the application with a start up check.
Usually you should specify default values for your configuration parameters if you want your application to run without the environment variable.
To get enviroment variables in Java you can use System.getenv();
I have a properties (jdbc.properties) file in "D:\UTL" location that needs to be created and defined in the weblogic startup script. Can someone help quickly ?
I have added like this which didn't work.
set UTIL=D:\UTL
Do I need to export this variable ? If yes, I am getting "export is not an internal /external command" error when I'm doing so. Appreciate your help.
If you wanted to export parameters, you can do so in the setDomain.sh. Specify your new variable and set it there in the same format as the previous variables.
You can do it like
NEW_VAR = "${NEW_VAR}"
export NEW_VAR
if you want to use a emote file, export what you need in the file and in the setDomain, run a source /path/to/file/file.sh
If this is application specific and you only want it to load for a particular application startup, please import property file in to application as and when it is required.
I am executing a simple program in java spring which uses "spring.xml" but eclipse is not able to determine location of this file. As I am sure that I am creating this file in root directory ie, SpringDemo simply providing spring.xml should work. Can anyone tell me what I am doing wrong here? I tried giving both path real and absolute but it did not work, ie ,the path information by right click->properties under resource
Structure of project:
You're trying to access spring.xml as a class path resource at the root of the classpath, but it is not on the classpath. Either add it to the classpath or use a FileSystemResource.