I assume that you would like to include the beans only if some "business feature" is available.
For example, you want to load a bean which is a DB driver only if this db is available at runtime.
Consider putting @ConditionalOnProperty
annotation on the beans to be included / excluded:
You'll end up with something like this:
@Configuration
public class ClientConfig {
@Bean
public Parser parser() {
return new Parser();
}
@Bean
@ConditionalOnProperty(name="validator.enabled", havingValue="true", matchIfMissing=false)
public Validator validator() {
return new Validator();
}
}
In this example, the Validator
bean will be loaded only if you've defined in the application.yml
the following property
validator:
enabled: true
Its also possible to put the bean on the entire configuration so that if you have a many inter-connected beans that make sense to load only together (or exclude them all):
@Configuration
@ConditionalOnProperty(name="client.enabled", havingValue="true", matchIfMissing=false)
public class ClientConfig {
@Bean
public Parser parser() {
return new Parser();
}
@Bean
public Validator validator() {
return new Validator();
}
}
@Conditional
-s are flexible mechanism, so if you don't want to rely on properties consider using other conditionals or even roll your own.
Update:
Based on op's comment:
Without changing the configuration itself, the only way I can think of is implementing a BeanFactoryPostProcessor
that can dynamically exclude the bean definition from the Application Context based on the custom logic. But I think its a fragile and error-prone way to work, so I wouldn't have done something like that in a real project.
The idea is simple: Bean Factory Post Processor runs before the beans actually get loaded and based on your logic (that you should implement), will remove the bean definitions populated from the configuration by spring during the start-up.
Check this link for example, it shows how to remove the bean