title: ComponentScan.basePackageClasses vs ComponentScan.basePackages to register a single Spring webMVC Controller?
body: <p>I want to add a single specific controller class to my Spring WebApplicationContext. I ran across the following example: (its in Scala, but is adapted from here: <a href="https://stackoverflow.com/questions/21022117/using-componentscan-or-contextcomponent-scan-with-only-one-class">using ComponentScan or context:component-scan with only one class</a>)</p>
  
  <pre><code>@Configuration 
  @ComponentScan(
    basePackages = Array("com.example.controllers"),
    useDefaultFilters = false,
    includeFilters = Array(
      new ComponentScan.Filter(`type` = FilterType.ASSIGNABLE_TYPE,
        value = Array(classOf[com.example.controllers.MyController]))))
  class MyConfig {
  }
  </code></pre>
  
  <p>This works nicely (but is very verbose).
  But Spring's @ComponentScan also has basePackageClasses</p>
  
  <pre><code>@Configuration 
  @ComponentScan( basePackageClasses=Array(classOf[com.example.controllers.MyController])) 
  class MyConfig {
  }
  </code></pre>
  
  <p>Of basePackageClasses, Spring's docs says:</p>
  
  <pre><code>Type-safe alternative to basePackages() for specifying the packages to
  scan for annotated components.
  </code></pre>
  
  <p>However, while the first ComponentScan correctly adds only com.example.controllers.MyController, but the second cause all of my @Controller to be scanned and added! Why? What's the use of basePackageClasses?</p>
  
  <p>Example like: <a href="https://github.com/mikaelhg/springmvc-example/blob/master/src/main/java/mikaelhg/example/ExampleConfiguration.java" rel="noreferrer">https://github.com/mikaelhg/springmvc-example/blob/master/src/main/java/mikaelhg/example/ExampleConfiguration.java</a>
  suggest that basePackageClasses can be used to load a single component.</p>
  
  <hr>
  
  <p>Update: </p>
  
  <p>As an aside, replacing:</p>
  
  <pre><code>@Configuration 
  @ComponentScan(
    basePackages = Array("com.example.controllers"),
    useDefaultFilters = false,
    includeFilters = Array(
      new ComponentScan.Filter(`type` = FilterType.ASSIGNABLE_TYPE,
        value = Array(classOf[com.example.controllers.MyController]))))
  class MyConfig {
  }
  </code></pre>
  
  <p>with</p>
  
  <pre><code>@Configuration 
  class MyConfig {
  
    @Bean
    var myController = new com.example.controllers.MyController
  
  }
  </code></pre>
  
  <p>it seems that MyController never gets gets connected to the servlet (the behavior changed to 404-NotFound) -- when added to a WebApplicationContext.</p>