这几天在搞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 >
< 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.....");
}
}
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();
}
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();
}
}
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();
}
}
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)