2

I'm wondering if spring default profile is always applied, assuming its properties are not overwritten by another active profile?

e.g. I have properties defined which are only relevant to the default profile. I want these properties ignored when any other profile is activated, the properties won't be overwritten by another profile, they just don't apply once another profile is active.

From my testing, it seems these properties are still being picked up when I activate another spring profile. I am defining these properties in my spring application properties file.

I have also defined a Bean with the @Profile("default") annotation and that Bean is not invoked when I activate another profile. But the properties I have defined in application.xml under spring.profiles: default are being applied.

Pete
  • 57,112
  • 28
  • 117
  • 166
joey_82
  • 41
  • 7

1 Answers1

0

check this solution posted here. you ll have to define 2 beans based on the profile in order to hold a reference to the different properties.file. in your case , the first one(default) would have the default values , and in the second you could simply override them or let any other default value on the variable

Community
  • 1
  • 1
AntJavaDev
  • 1,204
  • 1
  • 18
  • 24
  • thanks for response but it im still not clear on the answer to my question. what I really want to know is : Are configuration properties defined inside a default profile always applied unless they are overwritten by another active profile? from my testing, it seems they are. If the answer is Yes, I guess I have to overwrite these configuration properties with blank values inside the scope of other profiles, if I want them ignored? – joey_82 Apr 21 '16 at 13:32
  • yes , they are applied by default because you are injecting the beans with the `@Profile` annotation , so thats the solution described in the link i pasted , the difference is that , in the link , he uses the 2 different configuration , but in your case you want to simply put another property file , if someone passes another profile , to help you a bit more , could you describe what is your scenario ? – AntJavaDev Apr 21 '16 at 14:32
  • so for me scenario, I have an embedded h2 database created for my application when running my application locally, and I have defined some database properties for this database in the application yaml file, defined within the default profile. When I run my application elsewhere (Depoying it to a PaaS solution, I want to connect to a database instance, and those default properties are irrelevant and should not be applied to my 'real' database instance. But they are being picked up and causing errors. – joey_82 Apr 25 '16 at 07:50
  • well yes you might have a mis-configuration in your context , i have used the `@Profile` annotations for a same reason as you mentioned , in order to get different DBs contexts , and it was operating properly , the thing is , how are you exporting your web app each time. Are you using maven to set up the correct $property.Profile ??? – AntJavaDev Apr 25 '16 at 10:12
  • I'm using maven and specifying profile property on the CLI when running locally. I am setting the profile property via the application manifest when running on Paas. I know the profile is set in enviromnet as it is correctly picking up other properties specific to the profile. – joey_82 May 03 '16 at 08:24
  • have you specified 2 property file locations as well ? in order to be picked up by the different profiles? or you are simply autowiring the properties in each bean , no matter the profile – AntJavaDev May 03 '16 at 13:40