title: Spring @Transactional on @Bean declaration instead of class Implementation
body: <p>I'd like to configure "transactional" beans from my Spring <code>@Configuration</code> class instead of annotating the class implementation itself with <code>@Transactional</code>.</p>
  
  <p>Kind of like the old school way, configuring transactional advice from an XML file, but without needing a String reference to my class/method names to create pointcuts.</p>
  
  <p>The reason is that the bean implementation is in another code base, and the module it belongs to doesn't depend on Spring. Read : I'm not touching the source code of that bean, just instanciating it. The class is final, can't extend it either to add Spring annotations to the child class.
  Let's say that all the methods must be transactional, for simplicity.</p>
  
  <p>The bean implementation :</p>
  
  <pre><code>/** This class has no Spring dependency... */
  // @Transactional &lt;- which means I can't use this here
  public final class ComplexComponentImpl implements ComplexComponent {
  
      private SomeRepository repo;
  
      public ComplexComponentImpl(SomeRepository repository) { this.repo = repository }
  
      public void saveEntities(SomeEntity e1, SomeEntity e2) {
          repo.save(e1);
          throw new IllegalStateException("Make the transaction fail");
      }
  </code></pre>
  
  <p>What I want to do in my configuration class (and which doesn't work in my unit test) :</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  public class ComplexComponentConfig {
  
      @Bean
      @Transactional // &lt;- Make the bean transactional here
      public ComplexComponent complexComponent() {
          return new ComplexComponentImpl(repository());
      }
  
      // ...
  }
  </code></pre>
  
  <p>The example above doesn't work, indeed, as nothing gets "transactional" at runtime : entity <code>e1</code> is persisted even though the exception is thrown.</p>
  
  <p>Note that my transaction management setup works works perfectly well with an implementation class marked with <code>@Transactional</code>.</p>
  
  <p><strong>Question</strong> : Is is it possible to declare <code>@Bean</code>s transactional from a <code>@Configuration</code> class, or is there any alternative taking into accounts the constraints above ?</p>