Spring add declarative transaction

I. Introduction

Spring provides declarative transaction processing mechanism, it is all the work done in the full profile based AOP implementation, transaction management without having to write any code.

Two, XML configuration declarative transactions

Configuring transaction for the business section method, to use labels under tx and aop two namespaces, first import two namespaces in the Spring configuration file.

1, configure the transaction management component

Transaction Manager component provides comprehensive support and unified management of the transaction, in section equivalent to enhancing the role of treatment. As used herein, the transaction manager class DataSourceTransactionManager Spring provide to fulfill, as follows

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

When configuring DataSourceTransactionManager, to pre-defined data source assembly for injection

2, configure the transaction enhancement

By <tx: advice> configuration tag to enhance the transaction, the transaction manager to bind and define business rules for different methods

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="find*" propagation="SUPPORTS"/>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="del*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

Where the transaction-manager attribute refers to a transaction manager and Bean, the default value of transaction-manager attribute is transactionManager, that is to say, if the name of the transaction manager Bean is defined transactionManager, you can not specify the property value

<: Attributes tx> child tags to customize transactional attributes, transaction attribute by <tx: method> tag set by. Spring supports a different set of transaction attributes of different methods can be a <tx: advice> plurality <tx: method>

<tx: method> tag name attributes are required for the method to be matched. To carry out the agreement on the method name, you can use wildcards (*) . Other attributes are optional, used to specify the specific business rules, as follows

Temporarily omitted ......

3, the configuration section

Finally, the definition section, the transaction will enhance the entry point method combined with the business rules apply to the specified method

<! - defined aspects -> 
< AOP: config > 
    <! - defining pointcuts -> 
    < AOP: the pointcut ID = "serviceMethod" expression The . = "Execution (edu.cn.service .. * * * ( ..)) " /> 
    <! - the transaction entry point to enhance binding -> 
    < AOP: Advisor the advice-REF =" txAdvice " the pointcut-REF =" serviceMethod " /> 
</ AOP: config > 

aop: advisor's advice-ref property is referenced by: setting a component transaction attribute <tx advice> tag

Third, add annotations declarative transaction mode

Spring also supports the use of declarative transactions configuration annotations, annotation used is @Transactional. First still need to configure the transaction manager in the Spring configuration file, and add support for the transaction annotation configuration, as follows

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

After the above configuration, the program supports the use of @Transactional annotation of the transaction 

import org.springframework.transaction.annotation.Transaction
import org.springframework.stereotype.Service;

@Transaction
@Service("userService")
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transaction(propagation = Propagation.SUPPORTS)
    public List<User> findUsersWithConditions(User user){
        //省略
    }

    @Override
    public boolean addNewUser(User user){
        //省略
    }
}

Add @Transactional notes in business class to class for all business methods to add a unified transaction. If a business process requires different business rules can be added separately disposed @Transactional annotation on the service method.

@Transactional annotation can also set the value of the transaction attribute, the default settings are as follows @Transactional

Temporarily omitted ......

These default values ​​can be changed of course, follows

Temporarily omitted ......

Guess you like

Origin www.cnblogs.com/yanguobin/p/11707093.html