1. 程式人生 > >Spring aop 簡單示例

Spring aop 簡單示例

http () demo cut cti throwable ack 切面 aspectj

package com.yangxin.core.transaction;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class TransactionDemo2 {
    //定義切面
    @Pointcut(value="execution(* com.yangxin.core.service.*.*.*(..))")
    public void point(){
        
    } 
    @Before(value="point()")
    public void before(){
        System.out.println("transaction begin");
    }
    //把切點和通知關聯起來
    @AfterReturning(value = "point()")
    public void after(){
        System.out.println("transaction commit");
    }
    
    @Around("point()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("transaction begin");
        joinPoint.proceed();
        System.out.println("transaction commit");
        
    }
}

參考: https://www.cnblogs.com/cndota/p/6129244.html  

Spring aop 簡單示例