Tuesday, August 20, 2013

Spring Context PropertyPlaceholder and ActiveProfiles

Spring Context PropertyPlaceholder and ActiveProfiles

so here's a little design requirement that i ran into; i want to use the < context:property-placeholder /> with different configurations in my application context dependent on the Active Profile.  that is, in "production" i want it to read from a properties file, but for testing, i want to set all the values via properties object defined in Spring.

normally, with ActiveProfiles you can declare a bean definition in the regular bean namespace then define it again (with whatever is required) in the beans (profile) tag.  this doesn't work with the < context:property-placeholder /> as it doesn't have an id attribute.  (there's a bunch more sophisticated/accurate/clever reasons as to why, but that's the most readily identifiable).  as such, the profile won't override it, but instead create a second one.  no good for the design.

so here's the solution; default profile.  essentially we create all our beans, then create a default profile and inside it, declare our production < context:property-placeholder />. to declare our test or alternative profile, we just do the same again.


   hello world

the default profile is the profile picked up by "default" when the application context is started.  as such, we don't need to specify it and it is akin to not wrapping anything in profiles.  it helps us in this case where we don't want a particular element to be referenced at all if we have a profile in play.

No comments:

Post a Comment