Spring (vii) Dynamic agent

Source: Internet
Author: User
Tags throwable

In the previous blog briefly wrote a static proxy, here is the main talk about dynamic agents

There are two main types of dynamic agents

JDK Dynamic Agent cglib Dynamic Agent

What is the difference between these two agents?

(Summarize according to your own comprehension)

1.JDK Dynamic Agent

His characteristic is that the target object must have an interface

His essence is: An implementation class that creates an interface

The timing of his run: when the program runs

2.CGLIB Dynamic Agent

He is characterized by an agent that does not have an interface on a type

His essence is: to build a subclass of the target type in memory

The timing of his operation was: compile-time

After a brief introduction of these two agents, we use an example to see how to implement Dynamic proxy

Do the JDK dynamic agent first

To prepare an interface Isomeservice, the interface has a method Dosome (), and an implementation class Someserviceimpl for this interface, and overrides the method in which the code is as follows

 package demo15;  /*  * * Created by mycom on 2018/3/8.  */ public  interface   isomeservice{ public  void   Dosome ();}  
Package demo15; /*  */Publicclass  Someserviceimpl implements Isomeservice    {  publicvoid  dosome () {        System. out. println (" 10:10 review ");}    }

Using the JDK dynamic Proxy does not need to be configured in the configuration file, so it is now tested directly, but it is not possible to use a single test, but to test it using the main method.

Package Demo15;import Java.lang.reflect.invocationhandler;import java.lang.reflect.method;import Java.lang.reflect.Proxy;/** * Created by mycom on 2018/3/8.*/ Public classTest { Public Static voidMain (string[] args) {//First, create an implementation class for an interfaceFinal Someserviceimpl service=NewSomeserviceimpl (); //before calling the method, you want to log logs with dynamic agents, generate a dynamic proxy, and return an interfaceIsomeservice proxyinstance =(Isomeservice) proxy.newproxyinstance (Service.getclass (). getClassLoader (), Service.getclass (). GetInterf Aces (),NewInvocationhandler () {/** * * @param proxy Object * Methods of @param method target type                     * @param parameters of the args method * @return * @throws throwable */                     Publicobject Invoke (Object proxy, Method method, object[] args) throws Throwable {//record the logs here .System. out. println ("before====="); //call method.Method.invoke (Service,args);//equivalent to the method of executing the target typeSystem. out. println ("after======="); return NULL;        }                }); //to invoke methods in a method in a dynamic proxyProxyinstance.dosome (); }}

The results of the operation are as follows

This means that the runtime executes the Invoke method before executing the Dosome method in the interface, and the experiment succeeds!

Using the Cglib dynamic Agent to achieve the above results, how to implement it?

Then there is no need to create the interface, (here I use the above Someserviceimpl class, this class does not have to make changes), I directly tested, and the test is the same as above, using the main method test, without configuration file

Package Demo09;import Org.springframework.cglib.proxy.enhancer;import Org.springframework.cglib.proxy.methodinterceptor;import Org.springframework.cglib.proxy.methodproxy;import Java.lang.reflect.Method;/** * Created by mycom on 2018/3/8.*/ Public classTest { Public Static voidMain (string[] args) {Final Someserviceimpl service=NewSomeserviceimpl (); Enhancer Enhancer=Newenhancer ();        Enhancer.setsuperclass (Service.getclass ()); Enhancer.setcallback (NewMethodinterceptor () {/** * * @param o Proxy Object * Methods of @param method target type * @param objects parameters of the target method * @param Methodproxy proxy class method is a new parameter * @return * @throws throwable*/             PublicObject Intercept (Object o, Method, object[] objects, Methodproxy methodproxy) throws Throwable { System. out. println ("before=====");                Methodproxy.invoke (service,objects); System. out. println ("after====="); return NULL;        }        }); Someserviceimpl Proxy=(Someserviceimpl) enhancer.create ();    Proxy.dosome (); }}

The result is the same as above, this is the two ways to implement dynamic Proxy!

Spring (vii) Dynamic 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.