I'm trying to understand a scenario which seems quite simple to me when using spring xml application context but seems more vague when using annotations.
Let's say I have a class that looks something like this (this is all for the sake of example. Don't pay attention to class names, etc...):
@Component
public class BusinessLogicImpl implements BusinessLogic {
@Autowire
BeanThatTalksWithDataSource beanThatTalksWithDataSource;
@Autowire
BeanThatDoesSomeWork beanThatDoesSomeWork;
}
And then I have some other bean that uses this bean
@Component
public class MainLogicImpl implements MainLogic {
@Autowire
BusinesLogic businessLogic;
}
Let's assume that BeanThatTalksWithDataSource
has 2 implementations - one that does work with file system data source, and another that works with DB. Also the BeanThatDoesSomeWork
has 2 different implementations.
I want to create 4 different configurations that involves each combination of the 2 implementations of BeanThatTalksWithDataSource
with the 2 implementations of BeanThatDoesSomeWork
and at the end to load the correct variant to the MainLogicImpl
.
In xml it's fairly easy - I create 4 xml files that hold the 4 different possible contexts. What would be the equivalent of such thing when working with annotations?
I would like to emphasise that I'm talking about annotation and not @Configuration class. I'm trying to figure out if that's possible with annotations alone.
Annotate all the implementations with @Profile
and when starting your application set the active profiles. Now when component-scanning kicks in it will only pickup the beans which satisfy the active profiles.
Links
@Profile (javadoc)