AOP_切面声明(XML+注解)
2016-12-11 15:28
295 查看
一、XML配置
1.切面
2.定义通知方法
3.配置切面
对于有参数的通知,如这里的前置通知,通过expression种指定限制条件args以外,还需要用arg-name指定切点的[b]哪个参数引用[/b]
二、注解配置切面
使用注解创建切面是AspectJ5以后引入的新特性,大大减少了声明切面的xml配置量
1.注册org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator类,这里不用手动声明这个bean,只需要配置<aop:aspectj-autoproxy/>即可
2.使用@Aspect声明切面
切面1(需要切入方法参数的前置通知)
切面2(不需要切入方法参数的通知)
三、测试
打印结果
1.切面
package cn.xt.aop; import org.aspectj.lang.ProceedingJoinPoint; public class AspectTest { public void before(String name){ System.out.println("before,通过织入切面的方法传递参数=="+name); } public void after(){ System.out.println("after"); } public void around(ProceedingJoinPoint jp){ try { System.out.println("around before"); jp.proceed(); System.out.println("around after"); } catch (Throwable e) { System.out.println("around throwning"); e.printStackTrace(); } } public void throwing(){ System.out.println("throwing"); } public void returning(){ System.out.println("returning"); } }
2.定义通知方法
package cn.xt.aop; public class AdviceMethod { public void test(String name){ System.out.println("test excuted...."+name); } }
3.配置切面
<!-- 声明切面 --> <bean class="cn.xt.aop.AspectTest" id="aspectTest"></bean> <!-- 声明需要织入切面的方法 --> <bean class="cn.xt.aop.AdviceMethod" id="adviceMethod"></bean> <aop:config> <aop:pointcut expression="execution( * cn.xt.aop.AdviceMethod.*(..)) and args(name)" id="pc"/> <aop:pointcut expression="execution( * cn.xt.aop.AdviceMethod.*(..))" id="pc2"/> <aop:aspect ref="aspectTest"> <aop:before method="before" pointcut-ref="pc" arg-names="name" /> </aop:aspect> <aop:aspect ref="aspectTest"> <aop:after method="after" pointcut-ref="pc2" /> <aop:around method="around" pointcut-ref="pc2" /> <aop:after-returning method="returning" pointcut-ref="pc2" /> <aop:after-throwing method="throwing" pointcut-ref="pc2" /> </aop:aspect> </aop:config>
对于有参数的通知,如这里的前置通知,通过expression种指定限制条件args以外,还需要用arg-name指定切点的[b]哪个参数引用[/b]
二、注解配置切面
使用注解创建切面是AspectJ5以后引入的新特性,大大减少了声明切面的xml配置量
1.注册org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator类,这里不用手动声明这个bean,只需要配置<aop:aspectj-autoproxy/>即可
<aop:aspectj-autoproxy /> <!-- 声明切面 --> <bean class="cn.xt.aop.annotation.AspectTest"></bean> <bean class="cn.xt.aop.annotation.AspectTest2"></bean> <!-- 声明需要织入切面的方法 --> <bean class="cn.xt.aop.annotation.AdviceMethod" id="adviceMethod"></bean>
2.使用@Aspect声明切面
切面1(需要切入方法参数的前置通知)
package cn.xt.aop.annotation; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectTest { @Pointcut("execution( * cn.xt.aop.annotation.AdviceMethod.*(..)) && args(name)") public void pc(String name){} //声明带参数的通知 @Before("pc(name)") public void before(String name){ System.out.println("before,通过织入切面的方法传递参数=="+name); } }
切面2(不需要切入方法参数的通知)
package cn.xt.aop.annotation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AspectTest2 { @Pointcut("execution( * cn.xt.aop.annotation.AdviceMethod.*(..))") public void pc2(){} @After("pc2()") public void after(){ System.out.println("after"); } @Around("pc2()") public void around(ProceedingJoinPoint jp){ try { System.out.println("around before"); jp.proceed(); System.out.println("around after"); } catch (Throwable e) { System.out.println("around throwning"); e.printStackTrace(); } } @AfterThrowing("pc2()") public void throwing(){ System.out.println("throwing"); } @AfterReturning("pc2()") public void returning(){ System.out.println("returning"); } }
三、测试
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/xt/aop/annotation/aop.xml"); AdviceMethod am = ctx.getBean("adviceMethod",AdviceMethod.class); am.test("abc"); }
打印结果
before,通过织入切面的方法传递参数==abc around before test excuted....abc around after after returning
相关文章推荐
- Spring 在XML中声明切面/AOP
- <六>AOP面向切面——注解方式声明切面(附源码)
- Spring AOP使用整理:使用@AspectJ风格的切面声明
- AOP实现_AOP中的概念_注解方式_XML配置方式声明切面
- spring aop, aspectj 注解声明切面
- <七>AOP面向切面——配置bean.xml文件方式声明切面(附源码)
- Spring-AOP的五种通知和切面的优先级、通知变量声明
- Spring Aop之(二)--Aop 切面声明和通知
- Spring中基于aop命名空间的AOP 二(声明一个切面、切入点和通知)
- 黑马程序员_基础加强(7) 动态代理与AOP切面
- [置顶] spring切点切面aop——xml非注解
- 【Spring AOP】基于Schema配置切面
- AOP 同类方法调用无法进入切面
- 【第六章】 AOP 之 6.8 切面实例化模型 ——跟我学spring3
- AOP(面向切面)的粗俗理解
- Spring AOP切面日志Demo 配置文件方式
- 面向切面(AOP),原理是什么
- Spring面向切面(AOP)的例子
- Android面向切面(AOP)编程实战
- Spring学习总结之面向切面(AOP)