各种通知
- 介绍
- 代码
- ISomeService.java
- MyAspect.java
- MyTest.java
- SomeServiceImpl.java
- applicationContext.xml
介绍
前置通知
后置通知
环绕通知
异常通知
最终通知
代码
ISomeService.java
package com.study.annotation;// 主业务接口public interface ISomeService {// 目标方法 com.study.aop10.ISomeService.doFirstvoid doFirst();// 目标方法String doSecond();// 目标方法void doThird();}
MyAspect.java
package com.study.annotation;import org.aspectj.lang.JoinPoint;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.Before;import org.aspectj.lang.annotation.Pointcut;@Aspect //表示当前类为切面public class MyAspect {@Before(\"execution(* *..ISomeService.doFirst(..))\")public void myBefore() {System.out.println(\"执行前置通知方法\");}@Before(\"execution(* *..ISomeService.doFirst(..))\")public void myBefore(JoinPoint jp) {System.out.println(\"执行前置通知方法jp = \" + jp);}@AfterReturning(\"execution(* *..ISomeService.doSecond(..))\")public void myAfterReturning() {System.out.println(\"执行后置通知方法\");}@AfterReturning(value=\"execution(* *..ISomeService.doSecond(..))\",returning = \"result\")public void myAfterReturning(Object result) {System.out.println(\"执行后置通知方法 result = \" + result);}@Around(\"execution(* *..ISomeService.doSecond(..))\")public Object myAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println(\"执行环绕通知方法,目标方法执行之前\");// 执行目标方法Object result = pjp.proceed();System.out.println(\"执行环绕通知方法,目标方法执行之后\");if(result!=null) {result = ((String)result).toUpperCase();}return result;}@AfterThrowing(value=\"execution(* *..ISomeService.doThird(..))\")public void myAfterThrowing() {System.out.println(\"执行异常通知方法\");}@AfterThrowing(value=\"doThirdPointcut()\", throwing = \"ex\")public void myAfterThrowing(Exception ex) {System.out.println(\"执行异常通知方法 ex = \" + ex.getMessage());}@After(\"doThirdPointcut()\")public void myAfter() {System.out.println(\"执行最终通知\");}// 定义了一个切入点,叫doThirdPointcut()@Pointcut(\"execution(* *..ISomeService.doThird(..))\")public void doThirdPointcut(){}}
MyTest.java
package com.study.annotation;import org.aspectj.lang.JoinPoint;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.Before;import org.aspectj.lang.annotation.Pointcut;@Aspect //表示当前类为切面public class MyAspect {@Before(\"execution(* *..ISomeService.doFirst(..))\")public void myBefore() {System.out.println(\"执行前置通知方法\");}@Before(\"execution(* *..ISomeService.doFirst(..))\")public void myBefore(JoinPoint jp) {System.out.println(\"执行前置通知方法jp = \" + jp);}@AfterReturning(\"execution(* *..ISomeService.doSecond(..))\")public void myAfterReturning() {System.out.println(\"执行后置通知方法\");}@AfterReturning(value=\"execution(* *..ISomeService.doSecond(..))\",returning = \"result\")public void myAfterReturning(Object result) {System.out.println(\"执行后置通知方法 result = \" + result);}@Around(\"execution(* *..ISomeService.doSecond(..))\")public Object myAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println(\"执行环绕通知方法,目标方法执行之前\");// 执行目标方法Object result = pjp.proceed();System.out.println(\"执行环绕通知方法,目标方法执行之后\");if(result!=null) {result = ((String)result).toUpperCase();}return result;}@AfterThrowing(value=\"execution(* *..ISomeService.doThird(..))\")public void myAfterThrowing() {System.out.println(\"执行异常通知方法\");}@AfterThrowing(value=\"doThirdPointcut()\", throwing = \"ex\")public void myAfterThrowing(Exception ex) {System.out.println(\"执行异常通知方法 ex = \" + ex.getMessage());}@After(\"doThirdPointcut()\")public void myAfter() {System.out.println(\"执行最终通知\");}// 定义了一个切入点,叫doThirdPointcut()@Pointcut(\"execution(* *..ISomeService.doThird(..))\")public void doThirdPointcut(){}}
SomeServiceImpl.java
package com.study.annotation;// 目标类public class SomeServiceImpl implements ISomeService {@Overridepublic void doFirst() {System.out.println(\"执行doFirst()方法\");}@Overridepublic String doSecond() {System.out.println(\"执行doSecond()方法\");return \"abcde\";}@Overridepublic void doThird() {System.out.println(\"执行doThrid()方法\");throw new RuntimeException(\"这是一个测试异常\");}}
applicationContext.xml
<?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\" xsi:schemaLocation=\"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd\"> <!-- bean definitions here --><!-- 注册切面 --><bean id=\"myAspect\" class=\"com.study.annotation.MyAspect\"></bean><!-- 注册目标对象 --><bean id=\"someService\" class=\"com.study.annotation.SomeServiceImpl\"></bean><!-- 注册Aspectj的自动代理 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>