Spring2.0 AOP @AspectJ注解定义切面学习示例。

 

这几天在搞webwork spring,项目中用的webwork的拦截器来实现AOP的,顺便看了一下spring2.0的AOP。

 @AspectJ注解风格的方式确实要方便的多。

废话不多说。

几个关键的配置文件。

1.applicationContext.xml  (需要用spring2的配置方式来配置)

 

<? xml version="1.0" encoding="UTF-8" ?>

< 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"
       xmlns:tx
="http://www.springframework.org/schema/tx"
       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
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"

       default-autowire
="byName"  default-lazy-init ="true" >
  
< aop:aspectj-autoproxy />

   


    
<!--  ****************************** AOP TEST ************************** -->


    
< bean  id ="myAspect"  class ="com.hallywang.interceptors.MethodAspect" >

    
</ bean >


     
< bean  id ="test"  class  = "com.hallywang.Test" />


</ beans >

 2.对应的 MethodAspect.java,非常简单

 

package  com.hallywang.interceptors;

import  org.aspectj.lang.annotation.Aspect;
import  org.aspectj.lang.annotation.Before;
import  org.aspectj.lang.annotation.AfterReturning;

@Aspect
public   class  MethodAspect  {

    @AfterReturning(
"execution(* transfer(..))"//在transfer方法调用之后执行
    public void doAfterOne() {
        System.out.println(
"do doAfterOne.....");
    }




}

 

3.ITest.java,Test.java

 

package  com.hallywang;
public   interface  ITest  {
public abstract void doTest();
public abstract void executeTest();
    
public abstract void transfer();
}

 

 

package  com.hallywang;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.FileSystemXmlApplicationContext;
public   class  Test  implements  ITest  {
    
public void doTest() {
        
for (int i = 0; i < 10000; i++{
        }

    }


    
public void executeTest() {
        
for (int i = 0; i < 25000; i++{
        }

    }


    
public void transfer() {
        System.out.println(
"test.transfer()");
    }


    
public void testAOP() {
        ApplicationContext ctx 
= new
                FileSystemXmlApplicationContext(
"E:/work/webwork/defaultroot/WEB-INF/applicationContext.xml");
        
//只是为了测试,写了绝对路径...
        
        ITest test 
= (ITest) ctx.getBean("test");

        test.doTest();
        test.transfer();
    }


}

 

3. 测试主程序。

 

package  com.hallywang;

public   class  TestMain  {
    
public static void main(String[] args) {

        
new Test().testAOP();
    }

}

 

结果应该打出:

test.transfer()
do doAfterOne.....

 

有个问题一直没搞懂,我用spring对webwok中的某个DAO类进行切入,总是报错。

在spring创建action的bean的时候就报错

(这个action的bean里面被spring注入了该DAO)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值