Spring AOP is based on the principles of JDK dynamic proxies and cglib proxies and why JDK proxies need to be based on interfaces

Source: Internet
Author: User
Tags aop java web

This article explores what Spring AOP is based on the 13th chapter of deep Analysis of the insider of Java Web technology

In short, it's cut-face programming. The implementation of Spring AOP is implemented by the dynamic proxy of the JDK used by the interface, and is implemented by the proxy of the class using Cglib. JDK Dynamic Proxy

JDK Dynamic agent, is in the process of running the program, according to the interface of the agent to dynamically generate proxy class file, and load the running process. The purpose of the proxy is to invoke the Invocationhandler invoke method when invoking the target method, in fact Spring AOP is also here to make a fuss. It's also a typical proxy model.
The following simple code illustrates this problem to achieve its own invocationhandler

/**
 * * 
 * @author Yvan * */Public
class Aopproxy implements Invocationhandler {

    private Object Realobject;


    Public Aopproxy (Object realobject) {
        super ();
        This.realobject = Realobject;
    }


    @Override Public
    object Invoke (Object proxy, Method method, object[] args) throws Throwable {
        System.out.println ("Do something before execute Invoke method ...");
        Object invokeresult= Method.invoke (realobject, args);
        System.out.println ("Do something execute Invoke method ...");
        Return Invokeresult
    }

}
To develop interfaces and their implementation classes
/**
 * * 
 * @author Yvan
 */public
interface subobject {public
    string execute (String ... param);
}

/**
 * * 
 * @author Yvan * */Public
class Realobject implements Subobject {

    @Override
    public string Execute (string ... param) {for
        (int i = 0; i < param.length; i++) {
            System.out.println (param[i) );
        }
        Return ' Execute method ';
    }

Testing dynamic Agents
/**
 * * 
 @author Yvan
 * *  /Public
class Appmain {public
    static void Main (string[] args) {
        Subobject realobject = new Realobject ();
        Aopproxy aopproxy = new Aopproxy (realobject);
        Proxy generation Agent
        subobject subobject = (subobject) proxy.newproxyinstance (Aopproxy.getclass ()) provided through the JDK. getClassLoader (), Realobject.getclass (), getinterfaces ()
                , aopproxy);
        System.out.println (Subobject.getclass (). GetName ());
        System.out.println (subobject instanceof Proxy);
        String result =  Subobject.execute ("parameter One", "parameter Two");
        SYSTEM.OUT.PRINTLN (result);
    }
Print results
Com.sun.proxy. $Proxy 0
True do
something before execute Invoke method ...
Parameter
Two does
something after execute Invoke method ...
Execute method

To highlight: See Com.sun.proxy. $Proxy 0 is a dynamically generated proxy class for JDK, subobject instanceof Proxy is printed as ture stating that the proxy class inherits proxy.
Here is a question: Why JDK dynamic proxies must be based on interfaces
The reasons are as follows:
1, the generation of proxy class inherited proxy, because Java is a single inheritance, so can only implement interface, through the interface to achieve
2, from the design of Agent mode, make full use of the Java polymorphic characteristics, but also in line with the interface code based on the specification
Of course, the JDK also explains in the parameters of the build agent that a corresponding interface needs to be passed in

public static Object newproxyinstance (ClassLoader loader,
                                          class<?>[] interfaces,
                                          Invocationhandler h)
        throws IllegalArgumentException
Cglib Agent

Cglib bottom: Use bytecode to process the framework ASM to convert bytecode and generate new classes .
The Cglib (CODE generlize LIBRARY) proxy implements a proxy for a class, primarily by generating a subclass of the specified class, overwriting all of its methods, so that the class or method cannot declare final.

If the target object does not implement an interface, the Cglib proxy is used by default;
If the target object implements an interface, you can force the use of Cglib to implement the proxy.

Add the Cglib library and add <aop:aspectj-autoproxy proxy-target-class= "true" in the spring configuration/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.