Transaction management under Spring-using AOP @Transactional annotation Management

Source: Internet
Author: User
Tags aop class definition

Transaction management under Spring-using AOP @Transactional annotation Management

In addition to using XML configuration management, AOP has a way of transaction management: Using @Transactional annotation transaction management.
The class definition includes:

<!--the service class, we want to make transactional-
@Transactional public
class Purchaseserviceimp L implements Purchaseservice {

    Foo getfoo (String fooname);

    Foo Getfoo (String fooname, String barname);

    void Insertfoo (foo foo);

    void Updatefoo (foo foo);
}



When the above-mentioned POJO is defined in a Spring IoC container, the bean instance described above can be transactional with just a single line of XML configuration. As follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xmlns:tx  = "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd Http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-2.0.xsd "> <!--this is the service object, we want to make Tran Sactional--<bean id= "Purchaseservice" class= "Com.defonds.netbuy.purchase.service.PurchaseServiceImpl"/ > <!--enable the configuration of transactional behavior based on annotations--<tx:annotation-driven transaction-manager= "Txmanager"/> <!--A platformtransactionmanager is still required and <bean id= "Txmanager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <!--(this dependency is defined somewhere E LSE)--<property name= "DataSource" ref= "DataSource"/> </bean> <!--other <bean/> Defini tions--</beans>



        If it is a iBatis framework, refer to the previous blog, "Spring under Transaction Management-using AOP XML configuration management (IBatis as an example)" Support for related DAO.
        @Transactional annotations should only be applied to methods of public visibility. If you use @Transactional annotations on protected, private, or package-visible methods, it will not error, but this annotated method will not show the configured transaction settings.
        @Transactional Annotations can be applied to interface definitions and interface methods, class definitions, and public methods of classes. Note, however, that only @Transactional annotations appear to be less than the open transaction behavior, which is only a meta-data that can be used to identify @Transactional annotations and the beans that are configured appropriately to have transactional behavior. In the above example, it is actually the presence of the <tx:annotation-driven/> element that opens the transaction behavior. The recommendation of the
        Spring team is that you use @Transactional annotations on specific classes (or methods of classes) rather than on any interface that the class implements. You can certainly use @Transactional annotations on the interface, but this will only take effect if you set up an interface-based proxy. Because annotations are not inherited, this means that if you are using a class-based proxy, the transaction's settings will not be recognized by the class-based proxy, and the object will not be wrapped by the transaction proxy (it will be identified as critical). Therefore, accept the suggestions from the spring team and use @Transactional annotations on specific classes.
References: "Spring Framework reference 2.0.5".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.