Reputation: 54045
Let's say I have a HigherLevelBean
that depends on LittleService
. The LittleService
is an interface with two implementations.
There is no static or semi-static way to tell which of the two implementations should be used, it's all dynamic (session scoped, in fact). When request from this user comes in use the LegacyLittleService
, and for requests from that other user use NewShinyLittleService
.
The services are not going to be that small. They will have their own dependencies that will need to be injected and they will probably come from two different application contexts. Think about using one app over two different schemas/data models.
How can I achieve this kind of runtime dynamic? Ideally with annotation-driven configuration. What are my options, their pros and cons?
Upvotes: 3
Views: 816
Reputation: 692151
You could simply have a factory, where both services are injected:
@Component
public class LittleServiceFactory {
@Autowired
private LegacyLittleService legacy;
@Autowired
private NewShinyLittleService newShiny;
@Autowired
private TheSessionScopedBean theSessionScopedBean;
public LittleService get() {
if (theSessionScopedBean.shouldUseLegacy()) {
return legacy;
}
else {
return newShiny;
}
}
}
Now inject this factory anywhere you want, and call get() to access the appropriate LittleService instance.
Upvotes: 1