title: How to inject CrudRepository in Spring JPA?
body: <p>I'd like to make use of spring-data-jpa and <code>CrudRepository&lt;T, ID extends Serializable&gt;</code>. But I cannot get it autowired. (all other services in the same package are wired correctly):</p>
  
  <pre><code>@Service
  public class UserService {
      @Resource
      private UserRepo repo;
  
      //this does neither work
      //@Autowired
      //private CrudRepository&lt;User, Long&gt; repo;
  
  }
  
  public interface UserRepo extends CrudRepository&lt;User, Long&gt; {
  
  }
  
  @Entity
  public class User {
      @Id
      private Long id;
  } 
  </code></pre>
  
  <p>Result:</p>
  
  <blockquote>
    <p>Caused by: org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'userService': Injection of resource
    dependencies failed; nested exception is
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    qualifying bean of type [UserRepo] found for dependency: expected at
    least 1 bean which qualifies as autowire candidate for this
    dependency. Dependency annotations:
    {@javax.annotation.Resource(shareable=true, mappedName=, description=,
    name=, type=class java.lang.Object, lookup=,
    authenticationType=CONTAINER)}</p>
  </blockquote>
  
  <p>What might be wrong here?</p>
  
  <p>From the docs I think it should work without writing an implementation:</p>
  
  <blockquote>
    <p>In a typical Java application, you’d expect to write a class that
    implements CustomerRepository. But that’s what makes Spring Data JPA
    so powerful: You don’t have to write an implementation of the
    repository interface. Spring Data JPA creates an implementation on the
    fly when you run the application.</p>
  </blockquote>
  
  <hr>
  
  <p>Updated SpringConfig:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories("my.package")
  public class AppConfig {
  
      @Bean
      public LocalContainerEntityManagerFactoryBean emf() throws ClassNotFoundException, PropertyVetoException {
          LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
          emf.setDataSource(dataSource());
          emf.setPackagesToScan("my.package");
          emf.setJpaVendorAdapter(jpaAdapter());
          emf.setJpaProperties(jpaProterties());
          return emf;
      }
  }
  </code></pre>
  
  <p>Result: emf is missing, which is strange as I already have working DAO serices where I can autowire <code>EntityManager</code> and <code>EMF</code> without any problem.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1a6e658': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
      at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:632)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:442)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1094)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:989)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:276)
      ... 50 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
      ... 58 more
  </code></pre>