J2EE之巅

 

Spring2 AOP在使用XML配置时如何获得target及JoinPoint

蔡超

SCEA MCSD IBM RUP Specialist

Spring Reference 中介绍如何在采用 @AspectJ 方式在剖面中如何获取 target JoinPoint 并给出了示例,但并没有给出采用 XML 配置方式时介绍及示例,下面附上一个简单的小例子供大家参考。

package aop;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

 

/**

 * @author Administrator

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class LogAdvice1 {

       public void log(JoinPoint jp,MathImp imp){

 

              System.out.println("log:"+imp+" "+jp.toLongString());

             

       }

}

 

/*

 * Created on 2006-11-1

 *

 * TODO To change the template for this generated file go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

package aop;

 

/**

 * @author Administrator

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class MathImp /*implements Math*/{

 

       /* (non-Javadoc)

        * @see aop.Math#add(int, int)

         */

       public void add(int op1, int op2) {

              // TODO Auto-generated method stub

             

       }

 

       /* (non-Javadoc)

        * @see aop.Math#addtest(int, int)

        */

       public void addtest(int op1, int op2) {

              // TODO Auto-generated method stub

             

       }

 

       /* (non-Javadoc)

        * @see aop.Math#sub(int, int)

         */

       public void sub(int op1, int op2) {

              // TODO Auto-generated method stub

             

       }

 

}

 

配置:

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:aop="http://www.springframework.org/schema/aop"

      xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

 

   <!-- this is the object that will be proxied by Spring's AOP infrastructure -->

   <bean id="mathImp" class="aop.MathImp"/>

 

   <!-- this is the actual advice itself -->

   <bean id="logger" class="aop.LogAdvice1"/>

 

   <aop:config>

      <aop:aspect ref="logger">

 

         <aop:pointcut id="addLog"

                    expression="execution(* aop.MathImp.*(..)) and target(imp) and JoinPoint(jp)"  />

 

         <aop:before pointcut-ref="addLog"

                  method="log" arg-names="jp,imp" />

 

      </aop:aspect>

   </aop:config>

 

</beans>

测试

package aop;

 

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

 

/**

 * @author Administrator

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class Test {

 

       public static void main(String[] args) {

              AbstractApplicationContext context=new FileSystemXmlApplicationContext("aop2.xml");

              //Math math=(Math) context.getBean("math");

              MathImp math=(MathImp) context.getBean("mathImp");

              math.add(1,2);

              math.addtest(3,4);

              math.sub(5,6);

       }

}

posted on 2007-02-26 17:57 超越巅峰 阅读(3936) 评论(1)  编辑  收藏 所属分类: Java EE

评论

# re: Spring2 AOP在使用XML配置时如何获得target及JoinPoint 2007-03-30 18:45 itspy

good!!!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(12)

随笔分类(54)

随笔档案(59)

文章分类(2)

文章档案(1)

相册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜