zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.codingapi.tx.aop.service.impl;
 
import com.codingapi.tx.annotation.TxTransaction;
import com.codingapi.tx.aop.bean.TxTransactionInfo;
import com.codingapi.tx.aop.bean.TxTransactionLocal;
import com.codingapi.tx.aop.service.AspectBeforeService;
import com.codingapi.tx.aop.service.TransactionServer;
import com.codingapi.tx.aop.service.TransactionServerFactoryService;
import com.codingapi.tx.model.TransactionInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.lang.reflect.Method;
 
/**
 * Created by lorne on 2017/7/1.
 */
@Service
public class AspectBeforeServiceImpl implements AspectBeforeService {
 
    @Autowired
    private TransactionServerFactoryService transactionServerFactoryService;
 
 
    private Logger logger = LoggerFactory.getLogger(AspectBeforeServiceImpl.class);
 
 
    public Object around(String groupId, ProceedingJoinPoint point) throws Throwable {
 
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        Class<?> clazz = point.getTarget().getClass();
        Object[] args = point.getArgs();
        Method thisMethod = clazz.getMethod(method.getName(), method.getParameterTypes());
 
        TxTransaction transaction = thisMethod.getAnnotation(TxTransaction.class);
 
        TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
 
        logger.debug("around--> groupId-> " +groupId+",txTransactionLocal->"+txTransactionLocal);
 
        TransactionInvocation invocation = new TransactionInvocation(clazz, thisMethod.getName(), thisMethod.toString(), args, method.getParameterTypes());
 
        TxTransactionInfo info = new TxTransactionInfo(transaction,txTransactionLocal,invocation,groupId);
 
        TransactionServer server = transactionServerFactoryService.createTransactionServer(info);
 
        return server.execute(point, info);
    }
}