//配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--目标对象--> <bean id="someService" class="cn.happy.ASPECTJXM.SomeSevice"></bean> <!--增强:通知--> <bean id="myAspectj" class="cn.happy.ASPECTJXM.MyAspectj"></bean> <!--aop--> <aop:config> <!-- <aop:pointcut id="mypoint" expression="execution(* *..ASPECTJXM.*.*(..))"/>--> <aop:pointcut id="One" expression="execution(* *..ASPECTJXM.*.One(..))"/> <aop:pointcut id="Serend" expression="execution(* *..ASPECTJXM.*.Serend(..))"/> <aop:pointcut id="Thire" expression="execution(* *..ASPECTJXM.*.Thire(..))"/> <!--ref增强对象--> <aop:aspect ref="myAspectj"> <!--method增强方法--> <aop:before method="before" pointcut-ref="One"></aop:before> <aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="One"></aop:before> <aop:after-returning method="afterReturning" pointcut-ref="Serend"></aop:after-returning> <aop:after-returning method="afterReturning(java.lang.String)" pointcut-ref="Serend" returning="result"></aop:after-returning> <aop:around method="around" pointcut-ref="Serend"/> <aop:after-throwing method="afterThrowing" pointcut-ref="Thire"/> <aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="Thire" throwing="ex"/> <aop:after method="after" pointcut-ref="Thire"></aop:after> </aop:aspect> </aop:config> </beans>
//创建一个接口 public interface ISome { public void One(); public String Serend(); public void Thire(); } //创建一个接口的实现类 public class SomeSevice implements ISome { public void One() { System.out.println("One=================="); } public String Serend() { System.out.println("Send=============="); return "返回abc"; } public void Thire() { System.out.println("Third============"); int i=5/0;//制造假异常 } } //切面 public class MyAspectj {
//前置通知
public void before(){ System.out.println("前置通知方法before()"); } public void before(JoinPoint jp){ System.out.println("前置通知方法before() jp="+jp); }//后置通知
public void afterReturning(){ System.out.println("后置通知方法"); } public void afterReturning(String result){ System.out.println("后置通知方法 result="+result); }//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕通知方法,目标方法执行之前"); //执行目标方法 Object result = pjp.proceed(); //System.out.println(result); System.out.println("环绕通知方法,目标方法执行之后"+pjp); return ((String)result).toUpperCase(); }//异常通知
public void afterThrowing(){ System.out.println("异常通知方法"); } public void afterThrowing(Exception ex){ System.out.println("异常通知方法 ex="+ex.getMessage()); }//最终通知
public void after(){ System.out.println("最终通知方法"); } } //单测 @org.junit.Test //ASPECTJXML public void ASPECTJXML() { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextASPECTXML.xml"); cn.happy.ASPECTJXM.ISome servic= (cn.happy.ASPECTJXM.ISome)ctx.getBean("someService"); servic.One(); servic.Serend(); servic.Thire(); } //输出结果 前置通知方法before() 前置通知方法before() jp=execution(void cn.happy.ASPECTJXM.ISome.One()) One================== 环绕通知方法,目标方法执行之前 Send============== 环绕通知方法,目标方法执行之后execution(String cn.happy.ASPECTJXM.ISome.Serend()) 后置通知方法 result=返回ABC 后置通知方法 Third============ 最终通知方法 异常通知方法 ex=/ by zero 异常通知方法 java.lang.ArithmeticException: / by zero
通知
最新推荐文章于 2024-09-20 20:55:09 发布