aj通知函数

MyAspectj中的代码:

package com.gaojian.aspectj.test;

public aspect MyAspectj {
    /*调用某个文件夹下面所有的class*/
    //    pointcut callF(): execution(* com.gaojian..*.*(..));
   
    /**
     * 1) before : 在目标函数执行前执行
     * 2) after :在目标函数执行后执行
     * 3) after returning : 在目标函数返回是执行
     * 4) after throwing : 在目标函数抛出异常时执行
     * 5) around : 在目标函数执行中执行
     */
    pointcut callF(int begin) : execution(!static * com.gaojian.aspectj.test1.Function.*(int, ..)) && args(begin,..);
   
//    before(int begin) : callF(begin) {
//        System.out.println("*****************before*****************");
//    }
   
//    after(int begin) : callF(begin) {
//        System.out.println("*****************after*****************");
//    }
   
//    after(int begin) returning(int result): callF(begin) {
//        System.out.println("****************after return******************");
//        // 返回结果的输出
//        System.out.println(result);
//    }
   
//    after(int begin) throwing(Exception e) : callF(begin) {
//        System.out.println("抛出异常的类型:" + e.getClass().getSimpleName());
//        System.out.println("抛出异常的内容: " + e.getMessage());
//        System.out.println(e.toString());
//        System.out.println(thisJoinPoint.getSourceLocation());
//      System.out.println(thisJoinPoint.getStaticPart());
//    }
   
//    Object around(int begin) : callF(begin) {
//        if(begin == 0) {
//            begin = 1;
//        }
//        Object result = proceed(begin);
//        return result;
//    }
}

 

Function中的代码

package com.gaojian.aspectj.test1;

public class Function {
    public void f(int begin, int end) {
        System.out.println("method execution!");
    }
   
    public int f1(int begin) {
        System.out.println("method execution");
        return begin;
    }
   
   
    public void f2(int begin) {
        System.out.println("method execution");
        if(begin == 0) {
            throw new RuntimeException("begin can not be '0'!");
        }
    }
   
    public static void main(String[] args){
        Function function = new Function();
        /* 没有返回结果的函数执行 */
//        function.f(0, 1);
        /* 有返回结果的函数执行 */
//        function.f1(0);
        /* 有异常抛出 */
//        function.f2(0);
        /* around的使用 */
        /**
         * 输出的结果应该为0,经过切面中环绕函数的处理,结果变为了1。
         */
//        System.out.println(function.f1(0));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值