如何自己实现 Spring AOP?
以下内容来自公众号逆锋起笔,关注每日干货及时送达
转自:张喜硕
链接:https://segmentfault.com/a/1190000019148468
正好遇到了一道这样的题:抛开Spring来说,如何自己实现Spring AOP?就喜欢这样的题,能把那些天天写增删改查从来不思考的人给PK下去,今天就和大家一切学习代理模式与Spring AOP。
| 代理与装饰器
场景描述
宁静的午后,来到咖啡馆,想喝一杯咖啡。
基础实现
public interface Coffee {/*** 打印当前咖啡的原材料,即咖啡里有什么*/void printMaterial();}
public class BitterCoffee implements Coffee {@Overridepublic void printMaterial() {System.out.println("咖啡");}}
public class Main {public static void main(String[] args) {Coffee coffee = new BitterCoffee();coffee.printMaterial();}}

装饰器模式
/*** 糖装饰器,用来给咖啡加糖*/public class SugarDecorator implements Coffee {/*** 持有的咖啡对象*/private final Coffee coffee;public SugarDecorator(Coffee coffee) {this.coffee = coffee;}@Overridepublic void printMaterial() {System.out.println("糖");this.coffee.printMaterial();}}
SugarDecorator为咖啡加糖,最后把加好糖的咖啡给我。public class Main {public static void main(String[] args) {Coffee coffee = new BitterCoffee();coffee = new SugarDecorator(coffee);coffee.printMaterial();}}

Coffee coffee = new BitterCoffee(); // 点了一杯苦咖啡coffee = new SugarDecorator(coffee); // 给咖啡加了点糖
代理模式
iPad来到了咖啡馆,准备享受一个宁静的下午。public class CoffeeWithSugar implements Coffee {private final Coffee coffee;public CoffeeWithSugar() {this.coffee = new BitterCoffee();}@Overridepublic void printMaterial() {System.out.println("糖");this.coffee.printMaterial();}}
public class Main {public static void main(String[] args) {Coffee coffee = new CoffeeWithSugar();coffee.printMaterial();}}

差别
Coffee coffee = new BitterCoffee();coffee = new SugarDecorator(coffee);
Coffee coffee = new CoffeeWithSugar();
批评

Java中的接口可以让我们符合依赖倒置原则进行开发,降低耦合。用抽象类可以吗?可以。用类继承可以吗?也可以。| AOP
Spring AOP息息相关。AOP:Aspect Oriented Programming,面向切面编程,是面向对象编程的补充。如果你不明白这句话,好好去学学面向对象就知道为什么了。Spring AOP的实现就是代理模式。场景
public interface SMSService {void sendMessage();}public class SMSServiceImpl implements SMSService {@Overridepublic void sendMessage() {System.out.println("【梦云智】您正在执行重置密码操作,您的验证码为:1234,5分钟内有效,请不要将验证码转发给他人。");}}
public class Main {public static void main(String[] args) {SMSService smsService = new SMSServiceImpl();smsService.sendMessage();smsService.sendMessage();}}
费用统计
Spring的思路,肯定是声明一个切面,来切发短信的方法,然后在切面内统计短信费用。Spring来说,如何自己实现Spring AOP?JDK动态代理
InvocationHandler接口。Debug的时候都会跳转到invoke方法。public class MoneyCountInvocationHandler implements InvocationHandler {/*** 被代理的目标*/private final Object target;/*** 内部存储钱的总数*/private Double moneyCount;public MoneyCountInvocationHandler(Object target) {this.target = target;this.moneyCount = 0.0;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object result = method.invoke(target, args);moneyCount += 0.07;System.out.println("发送短信成功,共花了:" + moneyCount + "元");return result;}}
smsService替换为使用MoneyCountInvocationHandler处理的代理对象。public class Main {public static void main(String[] args) {SMSService smsService = new SMSServiceImpl();smsService = (SMSService) Proxy.newProxyInstance(Main.class.getClassLoader(),new Class[]{SMSService.class},new MoneyCountInvocationHandler(smsService));smsService.sendMessage();smsService.sendMessage();}}

InvocationHandler中的invoke方法动态生成一个类,该类实现SMSService接口,代理对象,就是用这个类实例化的。
AOP实现
AOP是不是也不是难事?IOC容器初始化中,扫描包,去看看哪些个类需要生成代理对象,然后构造代理对象到容器中。invoke方法里,把统计费用的逻辑改成切面的逻辑不就好了吗?不足分析
JDK的动态代理,是生成一个实现相应接口的代理类。但是Spring又不是只能通过接口注入。@Autowiredprivate Type xxxxx;
Spring的@Autowired是通过声明的类型去容器里找符合的对象然后注进来的,接口是类型,类不也是类型吗?@Autowiredprivate SMSService smsService;
@Autowiredprivate SMSServiceImpl smsService;
JDK动态代理针对直接注入类类型的,就代理不了。cglib动态代理
cglib。JDK动态代理解决不了的,统统交给cglib。@Autowiredprivate SMSServiceImpl smsService;
JDK动态代理解决不了。cglib怎么解决的呢?它会根据当前的类,动态生成一个子类,在子类中织入切面逻辑。cglib也不是万能的,方法是final的,子类重写不了,它当然也无计可施了。| 总结
IDEA 官方数据库管理神器,比 Navicat 还香?(文末赠书)

评论
