In my spring boot project, I have env property(application-dev.prop) file placed directly under resources folder, this prop file has some connection properties defined. The same connection properties with different value defined in user.properties file( which needs to be injected in a specific config class)placed under resources/config.I have annotated that specific config class with @PropertySource(value = "classpath:/config/user.properties"). But however this config class takes connection values from env property file but not from user.prop file even if that class annotated with property source user.property class path. This config class only accepts user.property value if a particular field not defined in env property file. In short it checks user.property file only if any field/property not defined in env.property file. But I want this config class to point user.property file always. I already checked spring boot precedence for scanning property file but couldn't resolve. Can anyone help me fix this issue.
Asked
Active
Viewed 377 times
1 Answers
0
As per the doc,
In cases where a given property key exists in more than one .properties file,
the last @PropertySource annotation processed will 'win' and override.
I think a better approach would be to use a profile if that fits your use case. The link here explains how to add profiles pretty neatly. you can check tht out.
Another approach I would suggest if you think adding a profile is not the right choice, you can make them passed down as environment variables. This gives you flexibility to change it as necessary and you can also give a default value for the environment variables.

Nithin
- 683
- 3
- 11
-
Thanks Nithin, Actually I need to point both property file. I need to point Env-prop file for my application and user.property file should be pointed/referenced only in a config class. Problem here is that config class still pointing to env-prop file even if I annotated that config class with @propertysource (value=classpath:/user.property). I am referring the properties which are present in both prop file and I cannot rename those property field(since they are predefined property name). – Priya Apr 10 '20 at 18:22
-
I tried setting system property approach, but that consider whichever property I am setting in env variables as universal for my application(not allowing me to refer another set which I want to refer) – Priya Apr 10 '20 at 18:26
-
I may have misunderstood your question earlier, you want same property loaded from both files and at different parts of the same project you are using that. So you need both at the same time but in different classes probably. Am I right? – Nithin Apr 10 '20 at 18:35
-
Right Nithin.Thats my use case. – Priya Apr 11 '20 at 03:11
-
I think there is no way other than to change the key of one of them if you want to populate it via spring. You can also read it as a normal properties file and use it. – Nithin Apr 11 '20 at 04:02