Spring AOP源码解析

2017-03-11  本文已影响79人  ibyr

欲了解AOP,需先了解Java动态代理;
欲了解Java动态代理,先熟悉设计模式之代理模式。
入门有道,先从简学。

1. 设计模式 之 代理模式

引言:消费者本应该直接从工厂购买商品,然而由于某些不可直达的原因,他选择从超市购买,超市代理了工厂销售商品的职责。并且,超市可以对商品进行再包装,工厂也可以专心于生产。

proxyPattern.png
/**
 * Common interface for Factory and Proxy.
 */
public interface  ICommon {
    public void doSomething(String msg);
}

/**
 * True object to be proxied.
 */
public class Factory implements ICommon {
    @Override
    public void doSomething(String msg) {
        System.out.println("Facotory do something: " + msg);
    }
}

/**
 * Proxy class
 */
public class Proxy implements ICommon {

    private ICommon iCommon;
    public Proxy(ICommon iCommon) {
        this.iCommon = iCommon;
    }
    @Override
    public void doSomething(String msg) {
        this.iCommon.doSomething(msg);
    }

    public void proxyDoOtherThing() {
        System.out.println("Proxy can do other things.");
    }
}

/**
 * Client class
 */
public class Client {
    public static void main(String[] args) {
        Factory factory = new Factory();
        ICommon proxy = new Proxy(factory);
        proxy.doSomething("Say Hello!");    // out: Facotory do something: Say Hello!
        proxy.proxyDoOtherThing();    // out: Proxy can do other things.
    }
}


2. Java动态代理
3. AOP设计

[待续]

上一篇下一篇

猜你喜欢

热点阅读