title: Spring 3: Autowire a bean declared in xml application context to a @Component bean
body: <p>I am having an issue with autowiring a bean property in my class declared as a <code>@Component</code>. I've tried a number of different things, but unfortunately I am getting a <code>NoSuchBeanDefinitionException</code> during spring configuration for this particular bean.</p>
  
  <p>Below is a some example code, to model what I have currently.</p>
  
  <pre><code>package com.foo.bar;
  
  @Component
  public class MyDeployer implements ApplicationContextAware
  {
      @Autowired
      private ClusterRegistry clusterRegistry; //The bean I am trying to wire
  
      ...
  }
  </code></pre>
  
  <p>In my application context, I have the following:</p>
  
  <pre><code>&lt;beans ...... /&gt;
      &lt;context:component-scan base-package="com.foo.bar" /&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;bean id="clusterRegistry" name="clusterRegistry" class="com.my.implementation.ClusterRegistryFileImpl" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My ClusterRegistryFileImpl class is defined below:</p>
  
  <pre><code>package com.my.implementation;
  
  public final class ClusterRegistryFileImpl implements ClusterRegistry
  {
      ...
  }
  </code></pre>
  
  <p>Note that the ClusterRegistryFileImpl implements the ClusterRegistry interface, and this interface/implementation class has no dependencies. These files also are not using component scan (and are not annotated by <code>@Component</code>, but I wouldn't think they'd need to be if I'm declaring the bean in my appContext.</p>
  
  <p>My integration test looks like</p>
  
  <pre><code>@ContextConfiguration(locations= {
      "/com/app/context/path/appContext.xml",
      "/com/app/context/path/aDifferentAppContext.xml"
  })
  public class MyDeployerTest extends AbstractTestNGSpringContextTests
  {
      @Autowired
      private MyDeployer deployer; //class that uses the bean I need
  
      @Test
      protected void testDeployerStartup()
      {
          deployer.startup();
          ...
      }
  
  }
  </code></pre>
  
  <p>Below is the error:</p>
  
  <pre><code>Caused by: 
    org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No qualifying bean of type [com.my.implementation.ClusterRegistry] found for dependency: 
    expected at least 1 bean which qualifies as autowire candidate for this dependency.
  Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>
  
  <p>Any suggestions or direction would be much appreciated.</p>