Spring's AOP (ii)----JDK Dynamic proxy __jdk dynamic proxy

Source: Internet
Author: User
Tags aop

The JDK dynamic proxy is introduced at 1.3, and its underlying need relies on a tool class Java.lang.reflect.Proxy and an interface Java.lang.reflect.InvocationHandler

JDK Dynamic proxy must be agent based on interface

code example:
Jdkproxyutils.java

Package com.my.jdkproxy;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;

Create JDK dynamic Proxy (tool Class) Public
class Jdkproxyutils implements Invocationhandler {

    //Proxy target object
    private object T for target object Arget; 

    Public jdkproxyutils (Object target) {
        this.target = target;
    }

    The target object method is invoked by invoke @Override public object
    Invoke (Object proxy, methods method, object[] args)
            throws Throwable {
        System.out.println ("Perform JDK agent enhancements ...) ");
        Return Method.invoke (target, args); Invoke target object Method
    }//

    create proxy for target object
    Createproxy () {
        //Loader object class loader
        //interfaces The target object implements
        the interface//h  callback object and requires Invocationhandler to execute the target object method return

        proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), this);

    }


Userdao.java

Package com.my.jdkproxy;

User Action Interface Public
interface Userdao {public
    void Save ();
    public void Update ();
    public void Delete ();

}

Userdaoimpl.java

Package com.my.jdkproxy;

User Action implementation class public classes
Userdaoimpl implements Userdao {

    @Override public
    Void Save () {
        SYSTEM.OUT.PRINTLN (save user ...) ");
    }

    @Override public
    Void Update () {
        System.out.println ("Update user ..."). ");
    }

    @Override public
    Void Delete () {
        System.out.println ("Delete user ...). ");
    }

}

Test.java

Package com.my.jdkproxy;


public class Test {

    //no agent Programming
    @org. Junit.test public
    void Demo1 () {
        System.out.println ("no agent Programming");
        Business Object
        Userdao Userdao = new Userdaoimpl ();

        Userdao.save ();
        Userdao.update ();
        Userdao.delete ();

        System.out.println ("----------------");
    }

    Apply JDK dynamic proxy
    @org. Junit.test public
    void Demo2 () {
        System.out.println ();
        SYSTEM.OUT.PRINTLN ("Application JDK dynamic Proxy");
        Target object
        Userdao Userdao = new Userdaoimpl ();

        Create JDK dynamic proxy for target object
        jdkproxyutils jdkproxyutils = new Jdkproxyutils (Userdao);//Incoming target object
        Userdao proxy = (Userdao ) Jdkproxyutils.createproxy (); Agent 

        //Invoke proxy object method must be created based on interface
        Proxy.save ();//Execute Invocationhandler Invoke method
        proxy.update ();
        Proxy.delete ();

    }



Run Result:

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.