title: Spring Data JPA - injection fails - BeanCreationException: Could not autowire field
body: <p>i followed the tutorial posted <a href="http://blog.springsource.org/2011/02/10/getting-started-with-spring-data-jpa/" rel="noreferrer">here</a> to get a basis application to work with Spring Data JPA. Now, how i understood, using the configuration</p>
  
  <pre><code>&lt;jpa:repositories base-package="my.package.to.scan" /&gt;
  </code></pre>
  
  <p>should result in that package beeing scanned by Spring Data JPA for interfaces extending <code>JpaRepository</code> and create a concreate bean of it so it can be used anywhere in my service classes using simple Spring <code>@Autowired</code>. But it fails, saying it can't find a bean with className (which is the default name the bean gets when created, simply using the de-capitalized ClassName).</p>
  
  <p>However, when i configure the bean manualy in my applicationContext like this:</p>
  
  <pre><code>&lt;bean id="ClassName" class="my.package.to.scan.ClassName"/&gt;
  </code></pre>
  
  <p>Spring is able to find the bean. I then of course get an error because i want to create a bean from an interface, which obviously can't work. BUT the point is that it seems like the Spring Data JPA "automatic bean creation" seems to fail somehow.</p>
  
  <p>I attached the relevant code so you can look at it. Btw, i should mention that i'm developing a portlet, so don't wonder why i dont have a spring-config. I'm currently using a applicationConfig only plus an MyPortlet-Portlet.xml for portlet configurations (but that should be not relevent for this problem). I added the import statements just to make sure i'm not using the wrong annotions / classes.</p>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;beans *** ALL MY XMLN's and XSI's *** /&gt;
  &lt;context:annotation-config /&gt;
  &lt;jpa:repositories base-package="model.repositories" /&gt;
  
  // JPA specific configuration here: dataSource, persistenceUnitManager exceptionTranslator, entityManagerFactory, SessionFactory, transactionManager - should not be relevant for this problem, tell me if i'm wrong
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  </code></pre>
  
  <p><strong>ICustomerService - just a interface for the CustomerService</strong></p>
  
  <pre><code>import model.entities.Customer;
  public interface ICustomerService {
          // example method
      public Customer getCustomer(Long customerId);   
  }
  </code></pre>
  
  <p><strong>CustomerService - the class used by my application logic to get / set ORM data</strong></p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.Pageable;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  import model.entities.Customer;
  import model.repositories.CustomerRepository;
  import model.service.interfaces.ICustomerService;
  @Repository
  @Transactional(readOnly = true)
  public class CustomerService implements ICustomerService{
      @Autowired
      private CustomerRepository repository;
  
      // example method
      @Override
      public Customer getCustomer(Long customerId){
          return repository.findById(customerId);
      }
  </code></pre>
  
  <p><strong>CustomerRepository - the repository for Spring Data JPA</strong></p>
  
  <pre><code>import javax.annotation.Resource;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.transaction.annotation.Transactional;
  import model.entities.Customer;
  @Resource
  @Transactional(readOnly = true)
  public interface CustomerRepository extends JpaRepository&lt;Customer, Long&gt;{
  
      public Customer findById(Long id);
  }
  </code></pre>
  
  <p><strong>Customer - my sample entity</strong></p>
  
  <pre><code>import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "Customers")
  public class Customer{
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name = "ID_CUSTOMER")
      private Long    id;
  
      @Column(name = "dbfirstname")
      private String  firstName;
  
      @Column(name = "dbname")
      private String  lastName;
  
      public Long getId(){
          return id;
      }
  
      public String getFirstName(){
          return firstName;
      }
  
      public void setFirstName(String firstName){
          this.firstName = firstName;
      }
  
      public String getLastName(){
          return lastName;
      }
  
      public void setLastName(String lastName){
          this.lastName = lastName;
      }
  }
  </code></pre>
  
  <p>i just came from classpath hell with WebSphere (damn, what a fu**ed up product) and now i'm here. hope somebody can help me with this.</p>
  
  <p>A basic explanation of what exacly goes wrong and maybe providing a better understanding of springs autowired injection feature would be great. I've read the spring documentation, but to say the truth: there are so many ways to configure something and it's not quite visible to me WHAT is really needed when choosing one of the config styles.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After trying to update the project i'm still getting the error. as requested here a little more details (trace):</p>
  
  <pre><code>Exception created : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private model.repositories.CustomerRepository model.service.CustomerService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
      [...]
          at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:522)
      at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1563)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private model.repositories.CustomerRepository model.service.CustomerService.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
      ... 96 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
      at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
      ... 98 more
  Caused by: java.lang.NullPointerException
      at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:73)
      at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:115)
      at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
      at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1215)
      at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:177)
      at org.hibernate.ejb.EntityManagerImpl.&lt;init&gt;(EntityManagerImpl.java:89)
      at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:179)
      at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:174)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:600)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517)
      at $Proxy325.createEntityManager(Unknown Source)
  
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:234)
      at $Proxy328.createNamedQuery(Unknown Source)
      at org.springframework.data.jpa.repository.query.NamedQuery.&lt;init&gt;(NamedQuery.java:74)
      at org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:96)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:128)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:71)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.&lt;init&gt;(RepositoryFactorySupport.java:303)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:157)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
      at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
  </code></pre>
  
  <p><strong>EDIT #2</strong>
  compleate applicationContext.xml (includeing the changes i made based on the ongoing discussion) added as requested</p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  
  &lt;jpa:repositories base-package="model.repositories" /&gt;
  
  &lt;context:component-scan base-package="model,model.repositories,model.service,controller" /&gt;
  
  &lt;bean class="model.service.CustomerService"/&gt;
  &lt;bean class="model.service.OrderService"/&gt;
  &lt;bean class="model.repositories.CustomerRepository"/&gt;
  &lt;bean class="model.repositories.OrderRepository"/&gt;
  
  
  &lt;bean id="myExceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" /&gt; 
  
  &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/mydata"
      resource-ref="true" cache="true" /&gt;
  
  
  &lt;bean id="pum"
      class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
      &lt;property name="persistenceXmlLocations"&gt;
          &lt;list&gt;
              &lt;value&gt;classpath*:META-INF/OverridePersistence.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="defaultDataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="database" value="MYSQL" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="persistenceUnitManager" ref="pum" /&gt;
      &lt;property name="persistenceUnitName" value="default" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="mySessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="model"/&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;value&gt;hibernate.dialect=org.hibernate.dialect.MySQLDialect&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
      &lt;property name="sessionFactory" ref="mySessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  </code></pre>