Spring2.0 AOP @AspectJ注解定义切面学习示例。
2007-07-20 16:14
926 查看
这几天在搞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)
@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)
相关文章推荐
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- spring aop, aspectj 注解声明切面
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-005-定义切面使用@Aspect、@EnableAspectJAutoProxy、<aop:aspectj-autoproxy>
- Spring 4.0 学习日记(8) ---AOP切面注解实现五种通知
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring学习-21:Spring的AOP:基于AspectJ的注解开发
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
- spring学习--面向切面编程AOP和AspectJ
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
- 在Intellij上面导入项目 & AOP示例项目 & AspectJ学习 & Spring AoP学习
- Spring学习--用 ASpectJ 注解实现 AOP
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
- Spring学习笔记:面向切面(AOP)的基本定义
- spring框架的学习(五)——Spring使用AspectJ进行AOP的开发:注解方式
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- (转)Spring使用AspectJ进行AOP的开发:注解方式