Spring AOP Summary part3

     more than ten days passed since last article published, sayings proves that half fogotten without three days practices, let`s continue to talk about the topic about aop, java jdk static proxy & java jdk dynamic proxy & java bytecode proxy.

      if you learn a lot about design pattens, you can easily know the implication about proxy patten. lets hava a short java code: 

public interface Connect{

    void connect();

}

it was easy to understand, a interface & and a method called connect()。 then we hava a class called Innernet, it cannot directly connect the Internet, so we should hava a ProxyNet to help Innernet to connect Internet. codes goes here:

public class Innernet implements Connect{


    @override
    public void connect(){
        Sys.out.pl("I want connect Internat")
    }

}
pub cls ProxyNet impl Connect{

    private Innernet inner;

    public ProxyNet(Innernet inner){
        this.inner = inner;
    }

    @Override
    public void connect(){
        Sys.out.pl("proxy before");
        inner.connect();
        Sys.out.pl("proxy after")
    }
        

}

so a static proxy patten was finished. how about a dynamic proxy, what we should understand, why dynamic ? & what make it can be a dynamic, let`s retalk about the problems about static proxy, if there has another classes should be proxy, how we should do in static proxy, we should write another interface and proxy class, as we know, we have a method called invoke(); so it`s a trrigle to make a dynamic proxy. all class needed to proxy can impl the InvocationHandler, codes like this:

pub cls ProxyBean impl InvocationHandler{

    T t;

    pub ProxyBean(T t){
        this.t = t;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args){
        Sys.out.pl("before proxy");
        Object result = method.invoke(this.t, args);
        Sys.out.pl("after proxy")
    }

}

so every thing goes well, but we have another question, why we hava a final class should proxy, how we should do, it can not inher any methods, then we have another way to realise proxy. that`s modify class file. try thinks this, you just want add some statements before or after a method, statements under method and before return , so you can modify the class file. it was a huge work to fully understand bytecode within java. you yould know asm & cglib. sorry i havn`t the reach the high.

你可能感兴趣的:(Java,Web,JAVA,Spring)