Acquaintance agent --Proxy

Ubiquitous mode --Proxy

       Recent looking at the "Zen Design Patterns", see chapter agency model, he found himself at the time of writing spring projects are very often used the agency, whether it is dependency injection, AOP or other, it can be said to nowhere It is not.

So they own their own way to write some small examples, of course, also made reference to other online bloggers blog, after all, a white hhhhhh

Into the title:

                            

                                        Figure 1-1: Proxy Proxy

 

 

Proxy mode mainstream there are now two categories: 1, JDK proxy 2, CGLB agent

(JDK static proxy did not say, the Internet has a lot of, I think the main business-oriented projects, the limitations of the static agent a little big for me, probably just to understand a little, but did not want to say this in depth )

 

Th1: JDK dynamic interface agent (the need to achieve InvocationHandler interface invoke (...) method, you must implement an interface)

public  class MyJdkProxy the implements of InvocationHandler { 

    // audience 
    public Object target; 

    // initialize the proxy object passed 
    public MyJdkProxy (Object target) {
         the this .target = target; 
    } 

    @Override 
    public Object Invoke (Object Proxy, Method, Method, Object [] args) throws the Throwable { 
        System.out.println ( "XSC" );
         return Method.invoke (target, args); 
    } 

    public Object newProxy () {
         // fetch agent of the target object instance 
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this
        );
    }
}

 

Th2: CGLB dynamic interface agent (need to achieve Intercept (...) method MethodInterceptor interface, of course, also be implemented without an interface, direct proxy class)

public  class MyCgljProxy the implements MethodInterceptor { 

    // audience 
    public Object target; 

    // initialize the proxy object passed 
    public MyCgljProxy (Object target) {
         the this .target = target; 
    } 

    @Override 
    public Object Intercept (Object O, Method, Method, Object [] objects, methodProxy methodProxy) throws the Throwable { 
        System.out.println ( "CJ" );
         return methodProxy.invoke (target, objects); 
    } 

    public Object newProxy () {
         // fetch agent of the target object instance
        return Enhancer.create(target.getClass(),this);
    }
}

 

Th3: Test class Test.java

public  class the Test {
     // interfaces to proxy 
    interface the Subject {
         void the sayHi ();
         void sayHallo (); 
    } 

    // proxy interface implementation class 
    static  class SubjectImpl the implements the Subject { 

        @Override 
        public  void the sayHi () { 
            System.out.println ( "the Hi" ); 
        } 

        @Override 
        public  void sayHallo () { 
            System.out.println ( "the Hello" ); 
        } 
    } 


    public  static void main(String[] args) {
        // JDK动态接口代理方式
        Subject target1 = new SubjectImpl();
        MyJdkProxy proxy = new MyJdkProxy(target1);
        target1 = (Subject) proxy.newProxy();

        target1.sayHallo();
        target1.sayHi();

        System.out.println("----------------------------");

        // CGLB动态接口代理方式
        Subject target2 = new SubjectImpl();
        MyCgljProxy myCgljProxy = new MyCgljProxy(target2);
        target2 = (Subject) myCgljProxy.newProxy();
        target2.sayHi();
        target2.sayHallo();
    }
}

 

Th4: Test Results

 

to sum up:

Of course, these are just simple proxy, in fact, feel a pass Belden, the goal is to provide a proxy for an object to control access to it, and our proxy class is entrusted preprocessing, filtering, forwarding messages, and subsequent processing after the commission to solve the problems directly access the object may bring

 

Revolution is not successful, comrades still work ~ ~ ~ ~

 

Guess you like

Origin www.cnblogs.com/xscc/p/11320473.html