SpringAOP 1.X中增强类的实现及织入创建动态代理的对象过程
一、增强实现类织入动态代理类的结构图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/92ffe9b3de1d12b4dc10c3ebc339c8fe.jpeg)
二、创建目标对象(被代理对象)接口及实现目标对象接口
(一) 创建目标对象接口
public interface UserInterface {
void showOne();
void showTwo();
}
(二) 实现目标对象接口
public class UserClass implements UserInterface {
@Override
public void showOne() {
System.out.println("one");
}
@Override
public void showTwo() {
System.out.println("two");
}
}
三、实现不同切入点增强类接口
(一) 实现前置增强接口
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(method.getName() + ":[" + new Date().getTime() + "];在目标对象方法之前执行!");
}
}
(二) 实现后置增强接口
public class AfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(method.getName() + ":[" + new Date().getTime() + "];在目标对象方法之后执行!");
}
}
(三) 实现环绕增强接口
public class BeforeAndAfterAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
long startTime = System.currentTimeMillis();
System.out.println("这是增强类织入在目标对象方法前的代码,2秒后执行目标对象的方法");
Thread.sleep(2000);
Object proceed = methodInvocation.proceed();
System.out.println("这是增强类织入在目标对象方法后的代码,2秒后将结束,并显示总共执行的时间段");
Thread.sleep(2000);
long endTime = System.currentTimeMillis();
System.out.println("执行完毕总共用时为:" + (endTime - startTime));
return proceed;
}
}
(四) 实现异常增强接口
public class ExceptionAdvice implements