连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出
目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
注2:完成切面编程代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知)
例子:外科医生+护士
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点
(也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)
二、前置通知
MyMethodBeforeAdvice:
package com.dzl.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
// target:目标对象
// method:被触发的目标对象的方法
// args:目标对象的目标方法的携带的参数
public void before(Method method, Object[] args, Object target) throws Throwable {
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "【系统日志】:正在调用->"+targetName+"."+methodName+",携带的参数:"+params;
System.out.println(msg);
}
}
priceException:
package com.lxy.aop.exception;
public class PriceException extends RuntimeException {
public PriceException() {
super();
}
public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public PriceException(String message, Throwable cause) {
super(message, cause);
}
public PriceException(String message) {
super(message);
}
public PriceException(Throwable cause) {
super(cause);
}
}
spring-context.xml:
拿到代理对象:
package com.dzl.aop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dzl.aop.biz.IBookBiz;
public class AopTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/spring-context.xml");
IBookBiz bookBiz=(IBookBiz) applicationContext.getBean("proxyFactoryBean");
// 这个buy是先去获取前置通知的代码,再去调用目标对象的业务代码
bookBiz.buy("张三", "西游记",9.9d);
bookBiz.comment("张三", "学七十二变");
}
}
三、后置通知
MyAfterReturningAdvice:
package com.dzl.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterReturningAdvice implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "【返利通知:返利3元】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params + ";目标对象所调用的方法的返回值:"
+ returnValue;
System.out.println(msg);
}
}
配置spring-context.xml:
四、环绕通知
价值:包含了前置通知与后置通知
MyMethodInterceptor:
package com.dzl.aop.advice;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
Object target = invocation.getThis();
Method method = invocation.getMethod();
Object[] args = invocation.getArguments();
// a.jsp window.open(b.jsp)
// b.jsp xxx->返回object->b.jsp window.close();window.getArguments;
String targetName = target.getClass().getName();
String methodName = method.getName();
String params = Arrays.toString(args);
String msg = "【环绕通知】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params;
System.out.println(msg);
Object returnValue = invocation.proceed();
String msg2 = "【环绕通知】:目标对象所调用的方法的返回值:" + returnValue;
System.out.println(msg2);
return returnValue;
}
}
配置spring-context.xml:
五、异常通知:
价值:处理异常数据,事务回滚
MyThrowsAdvice:
package com.dzl.aop.advice;
import org.springframework.aop.ThrowsAdvice;
import com.dzl.aop.exception.PriceException;
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(PriceException ex ) {
System.out.println("价格输入有误,购买失败,请重新输入!!!");
}
}
配置spring-context.xml:
一旦价格变成了负数
就会对应的去执行异常通知后面的代码
六、过滤通知:
配置spring-context.xml:
.*buy
com.dzl.aop.biz.IBookBiz
MyBefore myAfter2 MyFilterAdvice MyException
七、配置页spring-context.xm 所有代码如下:
吃饭 睡觉 打豆豆
.*buy
com.dzl.aop.biz.IBookBiz
MyBefore myAfter2 MyFilterAdvice MyException



