Springboot 註解聲名事務編寫&xml配置事務編寫
1. 註解聲名事務編寫
/** * springboot 之 註解式宣告事務,使用宣告式事務配置,可以有效規範程式碼 * @author Administrator */ @Configuration public class ApplicationContextTransactional { //事務方法超時時間設定 private static final int TX_METHOD_TIMEOUT //AOP切面的切點表示式 private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.baihoo.springboot.service.impl.*.*(..))"; //注入事務管理器 @Autowired private /** * 增強(事務)的屬性的配置 * isolation:DEFAULT :事務的隔離級別. * propagation : * read-only :false.不是隻讀 * timeout :-1 * no-rollback-for :發生哪些異常不回滾 * rollback-for :發生哪些異常回滾事務 * @return */ @Bean public TransactionInterceptor txAdvice() { /*增強(事務)的屬性的配置 * <tx:attributes> * */ NameMatchTransactionAttributeSource txAttributeS = new NameMatchTransactionAttributeSource(); /*propagation="REQUIRED" , timeout=5 ;rollback-for=".. , .."配置*/ RuleBasedTransactionAttribute requiredAttr = new RuleBasedTransactionAttribute(); requiredAttr.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); requiredAttr.setTimeout(TX_METHOD_TIMEOUT); requiredAttr.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); /*propagation="SUPPORTS" , readOnly="true"配置*/ RuleBasedTransactionAttribute supportsAttr = new RuleBasedTransactionAttribute(); supportsAttr.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); supportsAttr.setReadOnly(true); /* 注意:方法名稱來自類匹配的到方法 【save*, “*”表示匹配任意個字元】 <tx:method .../> */ Map<String , TransactionAttribute> txMethod = new HashMap<String , TransactionAttribute>(); txMethod.put("save*", requiredAttr); txMethod.put("add*", requiredAttr); txMethod.put("insert*", requiredAttr); txMethod.put("update*", requiredAttr);
txMethod.put("transfer*", requiredAttr); //對service層的轉賬業務方法開啟事務增強通知
txMethod.put("find*", supportsAttr); txMethod.put("select*", supportsAttr); txMethod.put("get*", supportsAttr); txAttributeS.setNameMap(txMethod); TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager , txAttributeS); return txAdvice; } /** * AOP配置定義切面和切點的資訊 * @return */ @Bean public Advisor txAdviceAdvisor() { AspectJExpressionPointcut pointcut= new AspectJExpressionPointcut(); pointcut.setExpression(AOP_POINTCUT_EXPRESSION); return new DefaultPointcutAdvisor(pointcut , txAdvice()); } } |
2. xml配置事務編寫
<?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:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 使用宣告式事務配置,可以有效規範程式碼 --> <!-- 配置事務管理器 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入連線池的物件,通過連線池物件建立模板. --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 宣告式配置事務通知 --> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <!-- 增強(事務)的屬性的配置 --> <tx:attributes> <!-- 注意:方法名稱來自類匹配的到方法 【save*, “*”表示匹配任意個字元】 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop配置定義切面和切點的資訊 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.baihoo.ssm.service.impl.*.*(..))"/> </aop:config> </beans> |