Talking about the proxy pattern of Java three ———— cglib agent

Source: Internet
Author: User

Then the proxy mode of the previous blog, we continue, the previous blog introduced the JDK dynamic agent, but the dynamic agent mechanism of the JDK can only implement the interface class, and can not implement the interface class can not implement the JDK dynamic proxy, so there are some limitations. In this case, we use cglib to achieve.

First, cglib dynamic agent

Cglib is the implementation of the proxy for the class, its implementation principle: the cglib of the bottom of the ASM bytecode generation framework, using bytecode technology to generate a proxy, than using reflection to generate a proxy, a subclass of the specified target class, and cover the method implementation enhancements. But there is a little bit of a disadvantage, because inheritance is used, so the final decorated class cannot be proxied.

or using the previous code, it is still a simple three-step implementation. First: Establish an ordinary business class; second: Write the Cglib proxy class; Third: Write the test code or the client call. The difference here is that in the first step, we don't need to build the interface, just an ordinary Java class.

Look at the code:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >/**        * Business class (including business logic)        *  i.e.: Delegate class       * @author Cassie */public          class account{                        @Override              public void Queryaccount () {                  System.out.println ("Query method ...");                        @Override public              void Updateaccount () {                  System.out.println ("Modify Method ...");}                    } </span>
then, let's write the Cglib proxy class:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >import Java.lang.reflect.Method;  Import Net.sf.cglib.proxy.Enhancer;  Import Net.sf.cglib.proxy.MethodInterceptor;    Import Net.sf.cglib.proxy.MethodProxy;      /** * Using cglib dynamic proxy * @author Cassie * Implement Methodinterceptor Interface */public class Cglibproxy implements Methodinterceptor {        Delegate object, run-time type private object target;          /** * Create proxy Object * * @param target * @return */public object getinstance (object target) {          This.target = target;          Enhancer enhancer = new enhancer ();          Enhancer.setsuperclass (This.target.getClass ());          Callback method Enhancer.setcallback (this);      Create proxy object return Enhancer.create (); } @Override//callback method public Object intercept (object obj, Method method, object[] args, method          Proxy proxy) throws Throwable {System.out.println ("before"); Object result = Proxy.invokesuper (obj, args);          System.out.println ("after"); return result}} </span>
last write test classcalled

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public class Testcglib {public            static void Main (string[] args) {//Instantiate proxy         cglibproxy cglib=new cglibproxy (); /through the agent to get the object account account         = [Account] Cglib.getinstance (new account ());          Account.query ();      }  } </span>

With the above code, we find that Proxy.invokesuper (OBJ,ARG) is the key to execution.

With CGLib, you need to implement the Methodinterceptor implementation class that CGLib gives us, and populate The Intercept () method. Method of the last Methodproxy type of parameter proxy, it is worth noting! CGLib provides us with a method-level proxy, which can also be understood as interception of methods. We call the proxy's Invokesuper () method directly, passing the Proxied object obj and the method parameter args into it.

At this point, the Cglib agent has also been implemented.


Talking about the proxy pattern of Java three ———— cglib agent

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.