You can use @Conditional from Spring4 or @ConditionalOnProperty from Spring Boot.
- Using Spring4 (only)
if you are NOT using Spring Boot, this can be overkill.
First, create a Condition
class, in which the ConditionContext
has access to the Environment
:
public class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment();
return null != env
&& "true".equals(env.getProperty("server.host"));
}
}
Then annotate your bean:
@Bean
@Conditional(MyCondition.class)
public ObservationWebSocketClient observationWebSocketClient() {
//return bean
}
2.Using Spring Boot:
@ConditionalOnProperty(name="server.host", havingValue="localhost")
And in your abcd.properties
file ,
server.host=localhost