Spring Java and Bean Configuration Defining profiles turned out to be exactly what I was looking for (thanks @ Adam-Gent and @ Guido-Garcia). The former seems necessary for a dynamic element, and the latter contributes to best practice.
Here is a solution with Java configuration and properties:
@Configuration public class SomeClassConfig { @Value("#{myProperties['enabled.subtype']}") public Class enabledClass; @Bean SomeInterface someBean() throws InstantiationException, IllegalAccessException { return (SomeInterface) enabledClass.newInstance(); } }
Here's a slightly less dynamic solution with profiles.
@Configuration @Profile("dev") public class DevelopmentConfig { @Bean SomeInterface someBean() { return new DevSubtype(); } } @Configuration @Profile("prod") public class ProductionConfig { @Bean SomeInterface someBean() { return new ProdSubtype(); } }
With profiles, an active profile is declared using one of various methods , for example, through a system property, JVM property, web.xml, etc. For example, with the JVM property:
-Dspring.profiles.active="dev"
John lehmann
source share