AOP_切面声明(XML+注解)
发布时间:2020-12-16 08:25:29 所属栏目:百科 来源:网络整理
导读:一、XML配置 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.printl
一、XML配置 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"); } }
package cn.xt.aop; public class AdviceMethod { public void test(String name){ System.out.println("test excuted...."+name); } }
<!-- 声明切面 --> <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>
二、注解配置切面 使用注解创建切面是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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |