Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Up Annotation Driven Transactions in Spring in @Configuration Class

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!

like image 214
Ian Dallas Avatar asked Sep 15 '10 22:09

Ian Dallas


People also ask

What is the use of @configuration annotation in Spring boot?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

What is @configuration annotation used for?

@Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

Which of the following is used to enable annotation driven transaction management?

Annotation Type EnableTransactionManagement. Enables Spring's annotation-driven transaction management capability, similar to the support found in Spring's <tx:*> XML namespace. To be used on @Configuration classes to configure traditional, imperative transaction management or reactive transaction management.

What is use of @transactional annotation in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.


1 Answers

You can now use @EnableTransactionManagement.

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

like image 98
AHungerArtist Avatar answered Oct 10 '22 15:10

AHungerArtist