title: NoSuchBeanDefinitionException when injecting javax.inject.Provider<T> to constructors using Spring
body: <p>I'm using <em>constructor-based dependency injection</em> to inject a <code>javax.inject.Provider&lt;T&gt;</code> to a service. With Spring Framework (4.2.5), <code>NoSuchBeanDefinitionException</code> will be thrown saying "No qualifying bean of type [T] found for dependency. Why is <code>T</code> expected by Spring while injecting <code>javax.inject.Provider&lt;T&gt;</code>?</p>
  
  <p>Here's the sample code:</p>
  
  <p><strong>Provider:</strong></p>
  
  <pre><code>import javax.inject.Provider;
  
  public class MessageProvider implements Provider&lt;String&gt; {
  
      @Override
      public String get() {
          return "Hello!";
      }
  
  }
  </code></pre>
  
  <p><strong>Service:</strong></p>
  
  <pre><code>import javax.inject.Inject;
  import javax.inject.Provider;
  
  public class GreetingService {
  
      private final String message;
  
      @Inject
      GreetingService(Provider&lt;String&gt; provider) {
  //     GreetingService(MessageProvider provider) { // this works!
          this.message = provider.get();
      }
  
      public String greeting() {
          return message;
      }
  
  }
  </code></pre>
  
  <p><strong>Test:</strong></p>
  
  <pre><code>import org.junit.Test;
  import static org.junit.Assert.*;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  public class GreetingServiceTest {
  
      @Test
      public void testSomeMethod() {
          AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
          ctx.register(MessageProvider.class);
          ctx.register(GreetingService.class);
          ctx.refresh();
  
          GreetingService bean = ctx.getBean(GreetingService.class);
          String message = bean.greeting();
  
          assertNotNull(message);
      }
  
  }
  </code></pre>
  
  <p>And here's the error message:</p>
  
  <blockquote>
    <p>Error creating bean with name 'greetingService': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [GreetingService]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}</p>
  </blockquote>