title: How does spring-boot @ConditionalOnClass work?
body: <p>How exactly does the <code>@ConditionalOnClass</code> annotation work?</p>
  
  <p>My goal is to load a specific bean only if the <code>jar</code> providing this class is included in the classpath.</p>
  
  <p>I thought the I could annotate a <code>@Bean</code> with <code>@ConditionalOnClass(MyService.class)</code> and declaring the dependency in maven as optional:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;de.my&lt;/groupId&gt;
      &lt;artifactId&gt;my-framework&lt;artifactId&gt;
      &lt;optional&gt;true&lt;/optional&gt;
  &lt;/dependency&gt;
  
  @Bean
  @ConditionalOnClass(MyService.class)
  public MyConditionalBean statistics() {
      return new MyConditionalBean();
  }
  </code></pre>
  
  <p>Now, anyone having <code>my-framework</code> as dependency should get that bean automatically wired. But anyone not having the dependency should skip it.</p>
  
  <p>But when I start the app, I get the following error:</p>
  
  <pre><code>Caused by: java.lang.ClassNotFoundException: de.MyService.class
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) ~[catalina.jar:7.0.50]
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) ~[catalina.jar:7.0.50]
  </code></pre>
  
  <p>So I'm probably doing something wrong. How can I then create the bean conditional on the dependency jars and the classes contained in the classpath?</p>
  
  <p>From the spring docs:</p>
  
  <blockquote>
    <p>The classes that must be present. Since this annotation parsed by
    loading class bytecode it is safe to specify classes here that may
    ultimately not be on the classpath.</p>
  </blockquote>
  
  <p>But the error states different...</p>