关于Schema-based AOP support 遇到的问题解决

来源:互联网 发布:c语言且或非符号 编辑:程序博客网 时间:2024/05/01 10:08
在以aspectj方式声明AOP的时候,spring官方的参考手册中的对args的用法一直都是这样
   <!-- this is the object that will be proxied by Spring's AOP infrastructure -->
   <bean id="fooService" class="x.y.service.DefaultFooService"/>
   <!-- this is the actual advice itself -->
   <bean id="profiler" class="x.y.SimpleProfiler"/>
   <aop:config>
      <aop:aspect ref="profiler">
         <aop:pointcut id="theExecutionOfSomeFooServiceMethod"
                    expression="execution(* x.y.service.FooService.getFoo(String,int))
                    and args(name, age)"/>
         <aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"
                  method="profile"/>
      </aop:aspect>
   </aop:config>
注意name,age都是getFoo方法的临时变量名,而我在开发的时候按照这样的写法却报错warning no match for this type name: XX。在我把name,age换成String, int这两种类型名称后,很意外却能正常工作,
我开发的代码如下:
<aop:pointcut id="prefixPointCut"
         expression="execution(* services.OcsServices.add*(..)) and args(prefix)" />
<aop:after pointcut-ref="prefixPointCut" method="afterAdd" arg-names="prefix" />
其中prefix是我另外定义的bean:
  <bean id="prefix" class="pojo.Prefix">
  </bean>
Advice触发的LogAop类的方法定义如下:
    public void afterAdd(Prefix p) {
        System.out.println(p.getMvnoname() +"  afterAdd execute......");
}
被Aop的类OcsServices的方法定义为:
public void add(Object o){
   …….
}
上面这些相关定义能正常的工作。但是当我将prefix改成o后(因为官方的定义都是定义为o,是临时变量名),即报异常warning no match for this type name: o;这个问题困扰了我3天,google了无数,论坛发帖求解,都无济于事,最后发现还是得靠自己。最后终于以官方教程得方式设置成功了,怎么做得呢?
<aop:pointcut id="prefixPointCut"
         expression="execution(* services.OcsServices.add(Object)) and args(o)" />
<aop:after pointcut-ref="prefixPointCut" method="afterAdd" arg-names="o" />
Advice触发的LogAop类的方法定义如下:
    public void afterAdd(Object o) {
        Prefix p = (Prefix)o;
        System.out.println(p.getMvnoname() +"  afterAdd execute......");
}
被Aop的类OcsServices的方法定义为:
public void add(Object o){
   …….
}
可以发现,所有得相关得参数都一致改成了o,很有意思,原来问题就在于我们必须对保持变量名一致,尽管这个变量运行在不同得方法不同得类中。
对于我们配置aop:after-returning这样可以捕获返回值得aop,目前也遇到了类似得问题,只是猜想类似而已,看配置:
      <aop:pointcut id="prefixPointCutQuery"
         expression="execution(* services.OcsServices.query(Object, int, int)) and args(Object, int, int)" />
      <aop:after-returning
         pointcut-ref="prefixPointCutQuery"
         returning="retVal"
         method="afterAddReturn"/>
Advice触发的LogAop类的方法定义如下:
    public void afterAddReturn(Object retVal){
        System.out.println(retVal);
}
被Aop的类OcsServices的方法定义为:
public List query(Object o, int page, int perpage){
    ………
}
上面这样得关联配置可以正常工作,但是当我改成这样后:
      <aop:pointcut id="prefixPointCutQuery"
         expression="execution(* services.OcsServices.query(Object, int, int)) and args(o, page, perpage)" />
同样报错warning no match for this type name: o;
但是当我改成:
      <aop:pointcut id="prefixPointCutQuery"
         expression="execution(* services.OcsServices.query(..))" />
却又是成功的。难道aop中的advice触发的方法没有使用o, page, perpage的话在args定义就会出现问题?这个原因没有能够得到官方的说法。