Spring2.0 AOP @AspectJ注解定义切面学习示例。
来源:互联网 发布:外贸网络推广营销 编辑:程序博客网 时间:2024/06/02 03:30
这几天在搞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)
- Spring2.0 AOP @AspectJ注解定义切面学习示例。
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- spring aop, aspectj 注解声明切面
- spring-aop---AspectJ注解式切面编程。
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring面向切面编程--AOP,AspectJ,基于注解方式。
- Spring AOP与AspectJ 面向切面编程配置与注解
- Spring AOP(三)——通过@AspectJ注解切面
- AspectJ Aop 面向切面
- spring2.0 配置AOP(切面)
- spring学习--面向切面编程AOP和AspectJ
- Spring AOP切面基于注解基础学习
- Spring AOP切面编程 AspectJ
- SpringAOP入门学习一:基于注解配置切面( AspectJ)
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- spring2.5AOP注解
- Spring2.5 Aop注解
- java地址收藏(持续更新中...)
- ASP.NET应用程序的安全方案(一)—身份验证
- ASP.NET应用程序的安全方案(二)—授权
- API 大全
- SQLserver中用convert函数转换日期格式
- Spring2.0 AOP @AspectJ注解定义切面学习示例。
- 最小化的窗口闪烁
- 遇到的Opera 奇怪问题
- Return 键转 Tab 键功能代码
- Web Service简介
- 常用正则表达式
- ASP.NET打开新窗口方法
- Fedora 安装配置
- Accessing the AutoCAD objects referred to by fields using .NET