关于ControlFlowPointcut与IntroductionInterceptor 【spring 2.0 技术手册补遗】

spring 2.0 技术手册 第4.3.3 ControlFlowPointcut示例中执行会抛出空指针异常,原因为Some.java中的setApplicationContext方法未被调用,则helloEverybody 方法被调用是helloProxy为null,在SpringAOPDemo.java的some实例化后加上"some.setApplicationContext(context);"此句即可。

spring 2.0 技术手册 第4.4.1 IntroductionInterceptor示例中,对OtherIntroduction类中被调用的org.aopalliance.intercept.MethodInvocation类补充说明:

org.aopalliance.intercept.MethodInvocation类继承至org.aopalliance.intercept.Invocation类,MethodInvocation类对一个方法的调用行为进行描述,并在方法被调用前插入一个拦截器。其中方法getMethod()方法返回的是被描述的方法的Method实例,getArguments()方法由父类继承而来,返回的是被描述方法的参数列表Object数组。

补充了相应解释的示例代码如下:

 
package  onlyfun.caterpillar;

import  org.aopalliance.intercept.MethodInvocation;
import  org.springframework.aop.IntroductionInterceptor;

public   class  OtherIntroduction  implements  IntroductionInterceptor, IOther  {
    
// 是否实现自IOther接口
    /**
     * (non-Javadoc)
     * 
     * 
@see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
     
*/

    
public Object invoke(MethodInvocation arg0) throws Throwable {
        
// 如果执行的方法来自IOther接口的定义
        
// comment by lecky
        
// MethodInvocation类的作用是对一个方法的调用行为进行描述,
        
// 并在方法被调用前插入一个拦截器
        if (implementsInterface(arg0.getMethod().getDeclaringClass())) {
            
// 执行额外加入(mixin)的行为
            return arg0.getMethod().invoke(this, arg0.getArguments());
            
// comment by lecky
            
// 此处arg0.getMethod()即为被调用的方法实例,arg0.getArguments()则是该方法的参数列表
        }
 else ...{
            
return arg0.proceed();
        }


    }

   
/**
     * (non-Javadoc)
     * 
     * 
@see org.springframework.aop.DynamicIntroductionAdvice#implementsInterface(java.lang.Class)
     
*/

    
public boolean implementsInterface(Class arg0) {
        
// TODO Auto-generated method stub
        return arg0.isAssignableFrom(IOther.class);
    }


    
/**
     * (non-Javadoc)
     * 
     * 
@see onlyfun.caterpillar.IOther#doOther()
     
*/

    
public void doOther() {
        
// TODO Auto-generated method stub
        System.out.println("增加的职责");
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值