A proper way to avoid explicit dependency on an API, while still using it if it's present
<p>While I was digging an issue in spring's sources, I found the following:</p>

<pre><code>public abstract class RequestContextHolder {
    private static final boolean jsfPresent = 
        ClassUtils.isPresent("javax.faces.context.FacesContext", 
        RequestContextHolder.class.getClassLoader());
</code></pre>

<p>and then in a method:</p>

<pre><code>if (jsfPresent) {
    attributes = FacesRequestAttributesFactory.getFacesRequestAttributes();
}
</code></pre>

<p>Where the factory is defined as :</p>

<pre><code>/**
 * Inner class to avoid hard-coded JSF dependency.
 */
private static class FacesRequestAttributesFactory
</code></pre>

<p>This works obviously, as the inner class is not 'touched' until it is required, and at that moment it is certain that JSF exists on the classpath.</p>

<p>Now, I wonder if this is a good approach to apply in a other cases. For example this method means a compile time dependency.</p>

<p>So, two questions:</p>

<ul>
<li>are there any drawbacks to this approach, other than the compile time dependency</li>
<li>are there better ways to prevent explicit dependency on an API, while still using it.</li>
</ul>
