Transaction Management in spring-use AOP @ transactional annotation Management

Source: Internet
Author: User

Transaction Management in spring-use AOP @ transactional annotation Management

In addition to xml configuration management, AOP also provides a transaction management method: Use @ transactional to annotate transaction management.
Class Definition:

<!-- the service class that we want to make transactional -->@Transactionalpublic class PurchaseServiceImpl implements PurchaseService {    Foo getFoo(String fooName);    Foo getFoo(String fooName, String barName);    void insertFoo(Foo foo);    void updateFoo(Foo foo);}

When the above pojo is defined in the Spring IoC container, the above bean instance can be transactional only through a line of xml configuration. As follows:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="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 that we want to make transactional -->  <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 -->  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <!-- (this dependency is defined somewhere else) -->    <property name="dataSource" ref="dataSource"/>  </bean>  <!-- other <bean/> definitions here -->  </beans>

For the ibatis framework, refer to the DaO support in the previous blog "transaction management under spring-use AOP xml configuration management (ibatis.
@ Transactional annotation should only be applied to the public visibility method. If you use the @ transactional Annotation on the protected, private, or package-visible method, no error is reported, but the annotated method does not display the configured transaction settings.
@ Transactional annotation can be applied to interface definition and interface method, class definition and class public method. However, note that only @ transactional annotation is insufficient to enable transaction behavior. It is only a metadata, it can be used by beans that can recognize @ transactional annotations and the preceding configurations with proper transaction behavior. In the preceding example, the transaction action is enabled for the <TX: annotation-driven/> element.
The spring Team recommends that you use @ transactional Annotation on a specific class (or class method) instead of any interface to be implemented by the class. Of course you can use the @ transactional Annotation on the interface, but this will only take effect when you set an interface-based proxy. Because annotations cannot be inherited, this means that if you are using a class-based proxy, the transaction settings cannot be recognized by the class-based proxy, and the object will not be wrapped by the transaction proxy (will be identified as serious ). Therefore, please accept the suggestions from the spring team and use the @ transactional Annotation on the specific class.
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.