Spring Summary VII: Implementation of an AOP dynamic agent

Source: Internet
Author: User
Tags aop

The AOP proxy in spring can either make the JDK dynamic proxy or the Cglib proxy, which is based on the interface, which is based on the subclass.

Let's start with the code to demonstrate the JDK dynamic proxy:

Now there is a product additions and deletions to check the operation

/*** Product Operation interface*/ Public InterfaceProductservice { Public voidAdd ();  Public voidedit ();  Public voidDelte ();  Public voidselect ();}/*** Implementation Class*/ Public classProductserviceimplImplementsProductservice {@Override Public voidAdd () {System.out.println ("Add Item"); } @Override Public voidedit () {System.out.println ("Modify Item"); } @Override Public voidDelte () {System.out.println ("Delete Item"); } @Override Public voidSelect () {System.out.println ("Search Products"); }}

We write a JDK-based dynamic proxy (implementing the Invocationhandler Interface):

 Public classJdkproxyImplementsInvocationhandler {//the target of being represented    PrivateObject Target; //constructor passed to target object     PublicJdkproxy (Object target) { This. target =Target; }    //provides methods for creating proxy objects     PublicObject Createproxy () {returnProxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), This); }    /*** Implement the callback method of the Invocationhandler interface, intercept the target object all methods will execute the Invoke method*/@Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("I am an agent, I have absolute control over the target of being represented ..."); Object ReturnVal=Method.invoke (target, args); System.out.println ("--------------------------------------"); returnReturnVal; }}

Test code:

 Public classjdkproxytest {@Test Public voidTestjdkproxy () {//Creating ObjectsProductservice target =NewProductserviceimpl (); //Create a proxy objectJdkproxy Jdkproxy =NewJdkproxy (target); //Agent-oriented interfaceProductservice proxy =(Productservice) jdkproxy.createproxy (); //calling methods by proxyProxy.add ();        Proxy.edit ();        Proxy.delte ();    Proxy.select (); }}

Operation Result:

Spring Summary VII: Implementation of an AOP 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.