java - Decide which JavaConfig class to load at runtime -
there multiple classes annotated @configuration , want decide in top-level config component 1 register in context.
@configuration public class filesystemdatastoreconfiguration { public @bean datastore getdatastore() { return new filesystemdatastore(withsomeconfigproperties); } } @configuration public class databasedatastoreconfiguration { public @bean datastore getdatastore() { return new databasedatastore(withsomeconfigproperties); } } @configuration public class datastoreconfiguration { // arbitrary logic decide whether want load // filesystem or database configuration. } i understand can use @profile select among multiple configuration classes. however, use profiles distinguish between environments. selection of configuration classes indepedent environment.
how can select @ runtime configuration class load?
can have multiple active profiles "production, withdatabase"?
if so, how can add profile based on property?
spring, there lots of ways things!
if stay annotations, can use @activeprofiles annotation enable overall set of profiles want:
@activeprofiles(profiles = profiledefinitions.my_enabled_profile) @contextconfiguration(as usual here...) you see "profiles" allows many profiles set. don't need store profiles constants either, may find helpful:
public class profiledefinitions { public static final string my_enabled_profile = "some-profile-enabled"; // can make profiles derived others: public static final string active_when_my_is_not = "!" + my_enabled_profile; } using of above, can selectively enable various configurations based upon dynamic setting of profiles:
@profile(profiledefinitions.my_enabled_profile) @configuration @import({these imported if profile active!}) public class databasedatastoreconfiguration { } @profile(profiledefinitions.active_when_my_is_not) @configuration @import({if necessary}) public class datastoreconfiguration { }
Comments
Post a Comment