后置通知:通知类实现 AfterReturnAdvice接口
异常通知:通知类实现 ThrowsAdvice接口
<!-- 管理StudentService组件对象-->
<bean class="com.pcf.service.StudentServiceImpl" id="studentService"></bean>
<!-- 注册通知-->
<bean class="com.pcf.advices.MyAfterAdvice" id="myAfterAdvice"></bean>
<aop:config>
<aop:pointcut id="afterPc" expression="within(com.pcf.service.StudentServiceImpl)"></aop:pointcut>
<aop:advisor advice-ref="myAfterAdvice" pointcut-ref="afterPc"></aop:advisor>
</aop:config>
package com.pcf.advices;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
/**
* @author yourname
* @date 2021/4/14 23:05
*/
public class MyAfterAdvice implements AfterReturningAdvice, ThrowsAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("===============进入后置通知=================");
System.out.println("执行的方法:" + method.getName());
System.out.println("方法的参数:" + args[0]);
}
// 处理异常时通知
public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
System.out.println("处理异常时通知");
System.out.println("ex.getMessage()" + ex.getMessage());
}
}