一、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>
对于有参数的通知,如这里的前置通知,通过expression种指定限制条件args以外,还需要用arg-name指定切点的哪个参数引用
二、注解配置切面
使用注解创建切面是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声明切面
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); } }
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原文链接:https://www.f2er.com/xml/294581.html