springboot - op 절단면 설정 전역 사무

4878 단어 자바springboot
package com.platform.generator.aspect;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;

@Slf4j
@Aspect
@Configuration
public class TxAdviceInterceptor {

    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.platform.service.impl.*.*(..))";
    /**
     *           
     */
    // private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.platform.service.impl.*.*(..))" +
    //         "                     || execution(* com.platform.generator.aspect.*.*(..))";

    private static final int TX_METHOD_TIMEOUT = 5000;

    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Bean
    public TransactionInterceptor txAdvice() {
        /*      ,            **/
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        /*    ,      */
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        /* transactiondefinition           PROPAGATION_NOT_SUPPORTED      5,      ,        ,        */
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
        /*             ,                */
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        /*           **/
        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        /*PROPAGATION_REQUIRED:      1,       ,      ;        ,         。     。 */
        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        /*        ,    5 ,     */
        requiredTx.setTimeout(TX_METHOD_TIMEOUT);
        Map txMap = new HashMap<>();

        txMap.put("save*", requiredTx);
        txMap.put("remove*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("batch*", requiredTx);
        txMap.put("clear*", requiredTx);
        txMap.put("add*", requiredTx);
        txMap.put("append*", requiredTx);
        txMap.put("modify*", requiredTx);
        txMap.put("edit*", requiredTx);
        txMap.put("insert*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("do*", requiredTx);
        txMap.put("create*", requiredTx);
        /*select,count     ,    ,         */
        txMap.put("select*", readOnlyTx);
        txMap.put("get*", readOnlyTx);
        txMap.put("valid*", readOnlyTx);
        txMap.put("list*", readOnlyTx);
        txMap.put("count*", readOnlyTx);
        txMap.put("find*", readOnlyTx);
        txMap.put("load*", readOnlyTx);
        txMap.put("search*", readOnlyTx);
        txMap.put("*", requiredTx);

        source.setNameMap(txMap);
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }

    @Bean
    public Advisor txAdviceAdvisor() {
        /*       :             。                    ——    、           * */
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        /*            ,       **/
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        /*    =  pointcut+  TxAdvice**/
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }
/**
    @Pointcut("execution(* com.platform.service.impl.*.*(..))")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //long beginTime = System.currentTimeMillis();
        //    
       // Object result = point.proceed();
        //    (  )
      //  long time = System.currentTimeMillis() - beginTime;
        //    
        log.info(" @Around*******************"+"     dong    "+"********************** @Around");
        return "";
    }
**/

}

좋은 웹페이지 즐겨찾기