Setting Up Annotation Driven Transactions in Spring in @Configuration Class

31,123

Solution 1

You can now use @EnableTransactionManagement.

See this post for more details: http://blog.springsource.com/2011/06/10/spring-3-1-m2-configuration-enhancements/

Solution 2

Take a look at http://blog.springsource.com/2011/02/17/spring-3-1-m1-featurespec. Spring 3.1's FeatureSpecification classes such as TxAnnotationDriven are designed to solve exactly the problem described above.

Solution 3

It seems like it isn't possible according to this forum post:

there may be a more first-class mechanism for enabling annotation-driven TX in @Configuration classes in Spring 3.1, but in the meantime, the recommended approach is to use @ImportResource to include a snippet of XML that declares <tx:annotation-driven/>

Wait: but you seem to have an XML context anyway. Why not add <tx:annotation-driven/> to it and use @Transactional?

Share:
31,123

Related videos on Youtube

Ian Dallas
Author by

Ian Dallas

Updated on May 11, 2020

Comments

  • Ian Dallas
    Ian Dallas almost 4 years

    So in the latest version of Spring we are able to use the @Configuration annotation to setup our configurations for Spring. Now in JavaConfig it is possible to use the @AnnotationDrivenTx (@AnnotationDrivenTx Reference Link) annotation to setup transactions in our Config class. But since JavaConfig has been decommissioned I was wondering if anyone knew how to setup something similar without JavaConfig and without needing to add anything to the application-context.xml. Here is what I basically have for my Config class

    @Configuration
    @ImportResource("config/application-context.xml")
    public class Config {
    
         public @Bean DataSource dataSource() {
               //get and return datasource
         }
    
         public @Bean Service1 getService1() {
              //return service1Impl
         }
    }
    

    And I'd like to make Service1 transactional. If anyone has any ideas on how to do this or if this is just not possible please let me know.

    Thanks!

  • Ian Dallas
    Ian Dallas over 13 years
    I ended up going this route. Would've been cool to do it the other way. Oh well.
  • Duncan McGregor
    Duncan McGregor over 13 years
    I too have been fighting to migrate a Spring 2.5 JavaConfig project to Spring 3. I appreciate the better integration, but many conveniences are gone.