Spring之Advice应用(下)

1、环绕通知:
这里写图片描述

环绕通知相当于在aop:aspect中配置了aop:before和aop:after-returning,其中Object retVal=pjp.proceed()这行代码前的相当于aop:before,此代码后的相当于aop:after-returning中的配置

2、Advice parameters
这里写图片描述

这里写图片描述

Advice parameters例子:
1、配置文件:

<aop:config>

        <aop:aspect id="moocAspectAOP" ref="moocAspect">

        <aop:around method="roundInit" pointcut="execution(* com.wuyonghu.spring2.AspectBiz.init(String,int)) and args(bizName,times)"/>
        </aop:aspect> 

    </aop:config>

2、切面中的方法:

public Object roundInit(ProceedingJoinPoint pjp,String bizName,int times) {
        System.out.println(bizName+" "+times);
        Object obj = null;
        try {
            System.out.println("around方法执行前");
            obj = pjp.proceed();
            System.out.println("around方法執行后");
        } catch (Exception e) {
            // TODO: handle exception
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return obj;
    }

3、切入点中的方法:

public void init(String bizName,int times){
        System.out.println("AspectBiz init:"+bizName+"   "+times);
    }

4、测试方法:

@Test
    public void test1() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        AspectBiz biz=(AspectBiz) context.getBean("aspectBiz");
        biz.init("海贼王",3);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值