AspectJ annotation Development AOP: define the cut-off point

Named as the cutoff point by @Pointcut, we facilitate unified management

In each notice within the definition of the cut point will cause heavy workload, difficult to maintain, for the repeated cut-off point can be defined using @Pointcut

Cut-point method: private void no-argument method, a method called cut roll call

When the notification plurality of cutting points, may be used to connect ||

Specific code:

Import org.aspectj.lang.ProceedingJoinPoint;
 Import org.aspectj.lang.annotation *. ; 

Import java.math.BigInteger; 

@Aspect 
public  class AspectJ { 
    @Before (value = "Execution (com.AspecJ.xiaomaoDao * * (. ..)) " )
     public  void before () { 
        System.out.println ( " I pre-notification! " ); 
    } 


    @AfterReturning (value =" Mypoint2 () ", returning =" Element " )
 //     use the returning to accept the return value 
    public  void the After (Object Element) { 
        System.out.println ( "I removed the" + element);//打印输出了返回值
    }

    @Around(value="MyPoint1()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object obj=null;
        System.out.println("环绕前");
        try {
            obj=joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("环绕后");
        return obj;
    }



    @AfterThrowing(value = "MyPoint()",throwing = "throwinfo")
    public void throwafter(Throwable throwinfo){
        System.out.println("发生了异常,异常信息如下:");
        System.out.println(throwinfo.getMessage());
    }



    @After(value = "MyPoint()")
    public void after(){
        System.out.println("最终通知执行了!");
    }

    @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.find())")
    public void MyPoint(){}

    @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.update())")
    public void MyPoint1(){}

    @Pointcut(value = "execution(* com.AspecJ.xiaomaoDao.delete())")
    public void Mypoint2(){}

}

 

Guess you like

Origin www.cnblogs.com/xiaolaha/p/11371509.html