Two implementations of the Java Dynamic Agent:

Source: Internet
Author: User
Tags throwable

Way one: the traditional agent

Package Cn.hc.domain;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import java.lang.reflect.proxy;/** * Factory class created by proxy object * @author HC * */public class Jdkproxyfactory implements invocationhandler{//is represented Objects Private Object Target;public jdkproxyfactory (object target) {This.target=target;} Create proxy public Object Createproxy () {//three parameters: Class loader implementation Interface Invocationhandlerreturn Proxy.newproxyinstance (Target.getclass () . getClassLoader (), Target.getclass (). Getinterfaces (), this);} public object Invoke (object arg0, Method arg1, object[] arg2) throws Throwable {//TODO auto-generated Method stub//call Target really. Method System.out.println ("Log has been recorded"); return Arg1.invoke (target, arg2);}}

The flaw in this approach is that the class being proxied must implement an interface

Package cn.hc.domain;//Proxy Object Interface public interface  Userdao {public void Save ();p ublic void Update ();} Package cn.hc.domain;/** * Target object of the agent * @author HC * */public class Userdaoimpl implements Userdao {public void Save () {//T ODO auto-generated Method StubSystem.out.println ("Add User"); public void Update () {//TODO auto-generated method stubSystem.out.println ("Update User");}} Test class Package Cn.hc.domain;import Org.junit.test;public class Testproxy {@Testpublic void Testjdkproxy () {//Test JDK dynamic agent// 1 Real target object Userdao userdao=new Userdaoimpl ();//system.out.println (Userdao); Userdao proxy= (Userdao) New Jdkproxyfactory (Userdao). Createproxy ();p roxy.save ();p roxy.update ();}}

Test results:

Log has been recorded

Add user

Log has been recorded

Update user

Mode 2:cglib Dynamic Agent

Package Cn.hc.cglibproxy;import Java.lang.reflect.method;import Org.springframework.cglib.proxy.enhancer;import Org.springframework.cglib.proxy.methodinterceptor;import Org.springframework.cglib.proxy.methodproxy;public Class Cglibproxyfactory implements Methodinterceptor{private object Target;public cglibproxyfactory (object target) { This.target=target;} Methods for creating Proxy objects public Object Createproxy () {//Create enhance Object Enhancer enhancer=new enhancer ();//cglib Create agent, Creates a subclass object Enhancer.setsuperclass (Target.getclass ()) on the target object, or the callback object, which is enhanced by the target enhancer.setcallback (this); Enhancer.create ();} Arg3 method for executing the parent class public Object intercept (object arg0, Method Arg1, object[] arg2,methodproxy arg3) throws Throwable {//TOD O auto-generated Method StubSystem.out.println ("Start recording log"); return Arg1.invoke (TARGET,ARG2);}}

Package Cn.hc.cglibproxy;public class Productdao {public void Add () {System.out.println ("add");} public void Delete () {System.out.println ("delete");}} Test method Package Cn.hc.cglibproxy;import Org.junit.test;public class Cglibtest {@Test public  void Test1 () {  // Create target object  Productdao dao=new Productdao ();  Productdao proxy= (Productdao) New Cglibproxyfactory (dao). Createproxy ();  Proxy.add ();  Proxy.delete ();  }}

Two implementations of the Java Dynamic Agent:

Related Article

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.