It seems ConditionalOnProperty
only works for properties within the classpath like application.properties
in the resources folder. I need a property that an end-user can turn on and off via an external property. An example is extremely simple:
Configuration class reads the external properties. Sys.out
to show it's reading the file properly.
@Configuration
@EnableAutoConfiguration
@PropertySource("file:/Users/end.user/MyApp/config/MyApp.properties")
public class PropertyConfigurer {
@Value("${featureOne}")
private String featureOne;
@PostConstruct
public void init() {
System.out.println("FeatureOne : " + featureOne);
}
}
Feature class, this component class will be put in the application context to be able to be used if the property is enabled via ConditionalOnProperty
, otherwise the component is never instantiated.
@Component
@ConditionalOnProperty(name="featureOne", havingValue = "true")
public class FeatureOne {
@PostConstruct
public void init() {
System.out.println("Feature initialized");
}
}
As you can imagine I am never seeing "Feature initialized" due to the "featureOne" property not being available to the spring context until after this class has been constructed. If there was some way to force the properties from @PropertySource
to be available to the spring context upon class instantiation. Or any other way? I also tried @DependsOn
the PropertyConfigurer
from FeatureOne
but that interestingly didn't work either.