title: Spring data repository @Autowiring is null when using @Transactional annotation on service bean
body: <p>I am studying spring framework and trying to use it in my project. But I have came across the following problem with spring data repository and @Transactional annotation  used in my service. The problem is that there are no exceptions on the spring start up. Later on when I try to access spring data repository I get NullPointerException. Maybe you have some thoughts that could help me.</p>
  
  <p>I am using spring data repository define as following:</p>
  
  <pre><code>package net.question.data.repository;
  
  import net.question.model.User;
  import org.springframework.data.jpa.repository.JpaRepository;
  
  public interface UserRepository extends JpaRepository&lt;User, Long&gt; {  
  }
  </code></pre>
  
  <p>Then I have a service defined which contains autowired repository:</p>
  
  <pre><code>package net.question.data.service;
  
  import net.question.data.repository.UserRepository;
  import net.question.model.User;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  @Service
  @Transactional
  public class UserService {
      @Autowired
      public UserRepository userRepository;
  
      public void doStuff(User usr) {
              // login will be here
      }
  }
  </code></pre>
  
  <p>here is the test to show my problem:</p>
  
  <pre><code>package net.question.spring;
  
  import static org.junit.Assert.assertNotNull;
  import net.question.data.service.UserService;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("/app-context.xml")
  public class InjectionTestSuite {
  
      @Autowired
      UserService userService;
  
      @Test
      public void testRepositoryInjection() {
              assertNotNull(userService);
              assertNotNull(userService.userRepository);
      }
  }
  </code></pre>
  
  <p>The test fails on the follwowing line:</p>
  
  <pre><code>assertNotNull(userService.userRepository);
  </code></pre>
  
  <p>If I remove the @Transactional annotation on the service then the test passes.</p>
  
  <p>here is my app-context.xml file:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:jpa="http://www.springframework.org/schema/data/jpa"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/data/jpa
      http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd "&gt;
  
      &lt;jpa:repositories base-package="net.question.data.repository" /&gt;
  
      &lt;!-- For discovering entity services --&gt;
      &lt;context:component-scan base-package="net.question.data.service" /&gt;
  
      &lt;bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
              &lt;property name="persistenceUnitName" value="hibernate_mysql" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager"     class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
              &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Maybe you have some ideas, how to find the error?</p>