静态代理,动态代理,AOP
静态代理:
public interface IUser { //保存用户 void save(); }
public class IUserImpl implements IUser{ @Override public void save() { System.out.println("模拟保存用户"); } }
//静态代理类,要与目标对象(IUserImpl)实现相同的接口 public class UserProxy implements IUser{ private IUser target = new IUserImpl(); @Override public void save() { System.out.println("代理操作: 开启事务..."); target.save(); // 执行目标对象的方法 System.out.println("代理操作:提交事务..."); } public static void main(String[] args) { IUser proxy = new UserProxy(); proxy.save(); } }
运行结果:

静态代理缺点: 如果增加一个方法,除了实现类需要实现这个方法外,所有的代理类也要实现此方法。增加了代码的维护成本。那么要如何解决呢?答案是使用动态代理。
动态代理:
接口:
package com.seeyii.web.yangkun.dynamicProxy;
public interface IVehicle {
void behavior();
}
实现类:
package com.seeyii.web.yangkun.dynamicProxy; /** * @PackageName:com.seeyii.web.yangkun.dynamicProxy * @Description: * @Author 杨坤 * @Date:2022/4/21 14:17 */ public class Car implements IVehicle { @Override public void behavior() { System.out.println("开汽车"); } }
调用处理类:
package com.seeyii.web.yangkun.dynamicProxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; /** * @PackageName:com.seeyii.web.yangkun.dynamicProxy * @Description: * @Author 杨坤 * @Date:2022/4/21 14:30 */ public class VehicleInvacationHandler<T> implements InvocationHandler { //被代理的对象 private T target; public VehicleInvacationHandler(T target){ this.target=target; } /* * @Author 杨坤 * @Date 14:34 2022/4/21 * proxy 代表动态代理对象 * method 代表正在执行的方法 * args 代表调用目标方法时传入的实参 **/ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("---------before-------"); Object invoke = method.invoke(target, args); System.out.println("---------after-------"); return invoke; } }
启动类:
package com.seeyii.web.yangkun.dynamicProxy; import java.lang.reflect.Proxy; public class App { public static void main(String[] args) { IVehicle car = new Car(); IVehicle vehicle = (IVehicle)Proxy.newProxyInstance(car.getClass().getClassLoader(), Car.class.getInterfaces(), new VehicleInvacationHandler(car)); vehicle.behavior();
// vehicle.behavior()会调用 VehicleInvacationHandler的invoke方法,invoke方法通过反射调用被代理类的对应方法
} }
运行结果:

如果被代理的类没有实现接口,可以用CGLib方式实现动态代理
AOP:
package com.seeyii.web.yangkun.aop; import com.seeyii.web.yangkun.dynamicProxy.Car; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.List; @Aspect @Component public class AopConfig { @Pointcut("execution( * com.seeyii.web.*.controller..*(..))") public void pointCut() { } @Around("pointCut()") public Object around(ProceedingJoinPoint joinPoin) { System.out.println("前置处理"); Object obj = null; try { //执行目标方法 obj = joinPoin.proceed();
System.out.println("后置处理");
} catch (Throwable throwable) { throwable.printStackTrace(); } return obj; } }
浙公网安备 33010602011771号