title: Anyway to @Inject/@Autowire an inner class into an outer class?
body: <p>In Spring/JSR-330, is there a way to properly declare an inner class which requires dependency injection, such that I can inject it into the outer class?</p>
  
  <p>For example:</p>
  
  <pre><code>@Component
  public class TestClass{
  
      // How to declare this class?
      private class TestClassInner{
         @Autowired private SomeBean somebean;
  
         public boolean doSomeWork(){
            return somebean.doSomething();
         }               
      }
  
      // Inject the inner class here in the outer class such that the outer class can use an instance of it
      @Autowired TestClassInner innerClass;
  
      @PostConstruct
      public void init(){
          ...
      }
  
      public void someMethod(){
         innerClass.doSomeWork();
         ...
      }
  }
  </code></pre>
  
  <p>I've tried annotating the inner class with @Component, making it a public class, making it public static, etc, but it seems that every combination I've tried always ends up throwing one error or another.</p>
  
  <p>As a private inner class, Spring complains that it is missing a constructor, even if I define one.</p>
  
  <p>As an annotated <code>@Component</code> public static class, Spring complains that it find two beans - TestClass@TestClassInner and testClass.TestClassInner.  And if I use a <code>@Qualifier</code>, it then complains that the bean cannot be found.</p>
  
  <p>I presume I am misunderstanding how these inner beans work/interact with Spring to properly understand if/how to declare them.</p>
  
  <p>Is this even possible?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>So here are a few combinations I've tried (including tried implementing a new constructor based on @SotiriosDelimanolis response):</p>
  
  <pre><code>    // How to declare this class?
  @Component
  public class TestClassInner{
      @Autowired private ProviderService providerService;
  
      public TestClassInner(){
          super();
      }
      public TestClassInner( TestClass t){
          super();
      }
  }
  </code></pre>
  
  <p>Throws error (both public and private inner class throw the same error):</p>
  
  <pre><code>Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ia.exception.TestClass$TestClassInner]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ia.exception.TestClass$TestClassInner.&lt;init&gt;()
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1030)
      ... 54 more
  </code></pre>
  
  <p>I just tried using a static public nested class with my test class (above) and it seems to inject properly.  On the other hand, in my actual controller, it discovers 2 matching classes:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.ia.web.ContractController$InnerClass] is defined: expected single matching bean but found 2: com.ia.web.ContractController$InnerClass,contractController.InnerClass
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:865)
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <pre><code>@Controller
  public class ContractController {
  
      @Component
      static public class InnerClass extends AttachmentControllerSupport{
  
          /**
           * 
           */
          public InnerClass() {
              super();
              // TODO Auto-generated constructor stub
          }
  
          public InnerClass( ContractController c){
              super();
          }
      }
  
      @Autowired private InnerClass innerclass;
  
      @Autowired private AttachmentControllerSupport attachmentControllerSupport;
      @Autowired private ContractService contractService;
  
  }
  </code></pre>
  
  <p>applicationContext.xml:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.ia"&gt;
      &lt;context:exclude-filter expression=".*_Roo_.*" type="regex"/&gt;
      &lt;context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/&gt;
  &lt;/context:component-scan&gt;
  &lt;context:spring-configured/&gt;
  </code></pre>
  
  <p>restmvc-config.xml:
      
      
          
          
      </p>
  
  <pre><code>&lt;mvc:annotation-driven conversion-service="applicationConversionService" &gt;
    &lt;mvc:argument-resolvers&gt;
      &lt;bean class="org.springframework.security.web.bind.support.AuthenticationPrincipalArgumentResolver" /&gt;
    &lt;/mvc:argument-resolvers&gt;
  &lt;/mvc:annotation-driven&gt;
  </code></pre>