title: How to configure spring boot application to use aspectj transactions?
body: <p>I would like to use transactions done by aspects in spring-boot aplication, but so far I mostly found help for more "ordinary" setups... </p>
  
  <p>What I've managed to set up so far?</p>
  
  <p>I've got those annotations on main class:</p>
  
  <pre><code>@SpringBootApplication
  @EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
  @EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
  </code></pre>
  
  <p>And this in my properties:</p>
  
  <pre><code>spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
  </code></pre>
  
  <p>I'm starting application with java agents spring-instrument and aspectj-weaver and I can see aspects in stacktrace where I've used <code>@Transactional</code> so at least this is working.</p>
  
  <p>Also, what is really strange, problem started only after I added second entity - first one worked without problem, but now I'm having this error:</p>
  
  <pre><code>org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
  </code></pre>
  
  <p>From what I've noticed beanFactory is <code>null</code> when <code>invokeWithinTransaction</code> is being run on <code>TransactionAspectSupport</code> - but it's being set during bean creation  (and from what I've seen in debug when <code>beanFactory</code> is <code>null</code> <code>TransactionAspectSupport</code> won't start a transaction because it isn't able to get <code>TransactionManager</code>). I guess non-bean instance of <code>TransactionAspectSupport</code> is being created at some point and that may be the reason for my problem (but then how and why?)?</p>
  
  <p>Update:</p>
  
  <p>From what I've noticed <code>@Transactional</code> is handled by <code>AnnotationTransactionAspect</code> in one case, but by <code>JtaAnnotationTransactionAspect</code> in another. </p>
  
  <p>Any ideas what I'm missing? Perhaps some magical property I should set?</p>
  
  <p>Oh, and here are my dependencies, if that is important:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-freemarker&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.h2database&lt;/groupId&gt;
          &lt;artifactId&gt;h2&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
          &lt;artifactId&gt;guava&lt;/artifactId&gt;
          &lt;version&gt;18.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;commons-io&lt;/groupId&gt;
          &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
          &lt;version&gt;2.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-instrument&lt;/artifactId&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
          &lt;version&gt;1.8.7&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
          &lt;version&gt;1.8.7&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>