Usually this kind of things can be achieved by using @Conditional
and its derivatives.
In one sentence this feature allows loading the bean (and making it available for injection into other beans) only if it obeys some condition. Usually its some kind of property or maybe presence of another bean, however in a nutshell you can define an implementation by yourself.
This is a tutorial about conditionals. More precisely you'll need a paragraph about the custom conditions
So you might define the condition that will "check the availability of the data source" and will return true
from its matches
method.
One caveat is that if you don't load, say DAO because the condition is not matched, then the code of the service that might use this Dao will fail because there is no bean of Dao available for injection.
So you might want either to load the no-op dao implementation when the condition is not met, or maybe not load the service itself, this really depends on the real project implementation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…