Cannot define proper path for #PropertySource annotation - java

I would like to use #PropertySource annotation in my Spring project. I do not know what shall be the path for my ceny.properties file. I have the following path,
#PropertySource("classpath:properties\\ceny.properties")
and dictionary structure,
C:\Users\X\IdeaProjects\XDxD\src\main\properties\ceny.properties
I am using IntelliJ. Where shall I put the ceny.properties file. I assume that the classpath is in main.

Related

Sping Boot - Separate config file other than application.yaml

I have a spring boot maven java application. I have the application.yml file and the properties specified here are used by a jar dependency of the project and I want to keep this file for them dependencies alone.
There is some additional config I want to use in the application. Is there a way to specify a second configuration file to be used within the project?
Thanks
You can use #PropertySource({"classpath:first.properties", "classpath:second.properties"}) like given below,
#Component
#PropertySource("classpath:config.properties")
public class MySeparateProperties
{
#Value( "${property.path.name}" )
private String prop;
}
Now use this bean to use the properties where ever you want in your application
The only issue is that, if the property path is same for application.yml and config.properties application.yml will have higher priority
Edit 1
If the file is located at resources/config/config.properties then you have to give classpath:config/config.properties
You can specify a different name using the property spring.config.name.
From the docs:
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.
Create property file as example x.properties and use #PropertySourse(“classpath:x.propeties”)
When need to use it
Configuration files can be set by spring properties, you can use:
spring.config.name to select the names of the configuration files (separated by comma)
spring.config.location to set the paths where the configuration files are located (separted by comma)
Try to look at springboot reference doc here for details.

Possible to change Path in another file than java class

I'm really new to Spring and got some questions.
One is.. in my Java class i've got a declaration of an absolute Directory path, where some Images are saved. Now if I want to send my Project to a co-worker or a friend, he needs to change the ImagePath in my Java Code. So I want to use a generalized technique for it, that there is a File where you can just edit the Path and the Project just works like it should.
I have heard of the method where you set up an application.yml and a Configuration Class, but I actually don't know, what to write in these Files.
There are two options for your problem:
One is to use a relative path like src/main/resources and put the images there.
Another option is to set the file path in application.properties file like:
my.images.directory.path=path
And now you can use this property in your classes using #Value:
For Example:
#Value( "${my.images.directory.path}" )
private String imagePath;
Same thing you can also set in a application.yml file.
Also, there are other ways to set the property in the properties file and use it, which you can refer over here.
Hope that helps.
Spring has a number of options when it comes to externalising configuration. In your case, the best option would be to add a property to the application.yml file. Ensure that you add this file to the classpath of your application. Alternatively you can specify the location of this file using spring.config.location command line argument.
Here is a simple application.yml file,
images:
path:/usr/images
You can then create a configuration bean in your application to inject this property.
#Component
public class MyConfig {
#Value("${images.path}")
public String name;
}
Now replace hardcoded path in your application with MyConfig.name.
For a full list of options for externalising configuration, check out this link.
You can use #PropertySource as well. Add a file in /src/main/resources, let's say config.properties containing properties.
config.properties file
images.dir=/path/to/images/dir
In java class, say #Controller add #PropertySource as shown in test code and you will be able to access the property.
You will only be required to update the file
#PropertySource("classpath:config.properties")
#Controller
public class TestController {
#Value("${images.dir}")
private String defaultDb;
...
}

Springboot #PropertySource with --spring.config.location=/etc/foo/

I'd like to completely externalize my config files so that every *.properties file is in /etc/foo/.
But I don't want everything in one big application.properties so I split certain parts away into individual *.properties files.
How can I tell #PropertySource that it should resolve the location of the given property file exactly as it would do for its application.properties i.e. search for it in every directly (could be multiple) that I specified with "--spring.config.location"?
My class would ideally look this way:
#Configuration
#PropertySource("dpuProfiles.properties")
#ConfigurationProperties("dpu.profiles")
public class DpuProfilesConfiguration {
...
}
It does work if I explicitly specify "file:/etc/foo/dpuProfiles.properties".
I couldn't get "${spring.config.location}/dpuProfiles.properties" or similar substitution with EL working. Makes probably no sense either as the variable could contain multiple directories.
You can do it by specify the the config(s) directory in your -classpath and point it from classpath to your #PropertySource
For your case it will be
CLASSPATH=just_added_as_an_example_jar.jar:/etc/foo/
-classpath "$CLASSPATH"
But you need to change the #PropertySource value as follows
#PropertySource({"classpath:dpuProfiles.properties"})
#ConfigurationProperties({"classpath:dpu.profiles"})
Note - The above example is to run from script.

Spring - how to override internal config file with external file

In my spring project I'm having two different conf files which have the same properties.
One of them is in an external directory while the other one is inside the resource of my project.
I want that the external file's properties to override the internal values (if they exist in the external file).
In my configuration class I set the files with the PropertySource annotation:
#PropertySources(
{ #PropertySource("file:${HOME}/conf/application.properties"),
#PropertySource("classpath:/data.properties")
})
By default the PropertySource is working like this:
- Read first property file
- Read second (...) property file - if it contains an already given key, it will be overridden.
Check this here: http://javapapers.com/spring/spring-properties-with-propertysource-annotation/
If you want to use it with XML, check also this answer: What is property resolution order in Spring property placeholder configurer with multiple locations?
Assuming you are working with spring-boot and have fat executable jar,
you can override internal config file properties with external with command line params as follow
java -jar your-executable-fat-jar.jar --spring.config.name=external-prop-file-name --spring.config.location=classpath:/application.properties,file://<external-config-file-parent-dir-path-NOT-EXTERNAL-CONFIG-FILE-PATH>
where
spring.config.name - external config file name
spring.config.location - Locations to look for configuration files
Note: with above config, spring-boot will look for external-prop-file-name.properties in external directory passed in spring.config.location
The order of the declared files is important. If the same key is defined in two or more files, the value associated with the key in the last declared file will override any previous value(s).
From spring docs:
In cases where a given property key exists in more than one .properties file, the last #PropertySource annotation processed will 'win' and override.
So just swap #PropertySource("file:${HOME}/conf/application.properties") with #PropertySource("classpath:/data.properties")
Example:
#PropertySources({#PropertySource("classpath:/data.properties"),#PropertySource("file:${HOME}/conf/application.properties")})

Spring not able to find classpath resource with #PropertySource

I am using #PropertySource in my datasource configuration file to get property files located on classpath. Below is my project structure.
I believe I can do it in two ways:
By creating a package in src folder and add them there. As src folder is already included in the classpath in eclipse, following should work.
#PropertySources({
#PropertySource("classpath: com/spring/property/general.properties"),
#PropertySource("classpath: com/spring/property/hibernate.properties")
})
Second way is to create a resources folder and add it to the classpath and following should work
#PropertySources({
#PropertySource("classpath: general.properties"),
#PropertySource("classpath: hibernate.properties")
})
In my case neither of the two is working. Being an intermediate java developer this still confuses me. Can anybody guide me in the right direction. And also how we can configure classpath resources for Spring in a production environment.
EDIT:
I have changed my project structure to include properties file in src/java/resources and I can see the resources folder in build path. Still .properties are not found by spring.
For anybody facing problem with usign .properties files in Spring 4+, look at the thread below to match your setup with that of OP. Setup is all good except for a whitespace in configuration.
Not able to inject .properties file into Spring MVC 4.3 using #PropertySource

Categories