title: Spring Java config, @Autowire vs. Constructor Injection, @Transactional and CGLIB
body: <p>We've been using <code>@Autowired</code> plus Java-based Spring configuration with some success but now, we're losing control. Everyone is starting to add autowired dependencies everywhere, creating cycles and strange bugs.</p>
  
  <p>So we are considering using constructor injection and autowiring of Spring configurations.</p>
  
  <p>Old:</p>
  
  <pre><code>class Bean {
     @Autowired Foo foo;
  }
  
  @Configuration
  @Import( FooCfg.class )
  class BeanCfg {
     @Bean public Bean bean() { return new Bean(); }
  }
  </code></pre>
  
  <p>New:</p>
  
  <pre><code>class Bean {
     public Bean(Foo foo) {...}
  }
  
  @Configuration
  class BeanCfg {
     @Autowired FooCfg fooCfg;
     @Bean public Bean bean() { return new Bean(fooCfg.foo()); }
  }
  </code></pre>
  
  <p>This works pretty well (and it drives people to split beans instead of creating monsters with 10+ constructor arguments).</p>
  
  <p>But it fails when <code>Bean</code> has a method annotated with <code>@Transactional</code> since CGLIB then tries to create a proxy which fails since it can't find a no-argument constructor.</p>
  
  <p>What's the solution for this?</p>