JAVA Dynamic Proxy (Interceptor) __java

Source: Internet
Author: User

This instance is an impersonation security check

The proxy object interface defines Java code public interface Usermanager {public void Aaduser (user user);       public void Deletebyid (int id);       public void UpdateUser (user user);   Public User FindByID (int id); }

Implement Java code    public class usermanagerimp implements usermanager {  by proxy object interface        @Override        public void aaduser (User  user)  {           system.out.println ("---------- AddUser------------");       }        @Override         public void deletebyid (int id)  {            system.out.println ("----------Deletebyid------------");        }        @Override        public  user findbyid (int id)  {            System.out.println ("----------FindByID------------");            return null;       }        @Override         public void updateuser (user user)  {            system.out.println ("----------updateuser------------");        }  }  

 

  Define a User object Java code    public class user {        private int id;       private String name;        private int age;       public String  GetName ()  {           return name;       }       public void setname (String name)  {           this.name = name;       }       public int getage ()  {           return age;       }        public void setage (int age)  {            this.age = age;       }        Public int getid ()  {           return id;        }       public void setid (int  ID)  {           this.id = id;       }  }  

 

  Inherit Invocationhandler interface Java code    package hb.proxy;      import  java.lang.reflect.invocationhandler;   import java.lang.reflect.method;   import  java.lang.reflect.proxy;     /*   * 1, dynamic proxy class must implement Invocationhandler interface, To implement the Invoke () method    */   public class securityhandler implements  invocationhandler {       private Object targetObject;       /*       *  The following method is to get the proxy object, if not the proxy object, this effect is not useful        *  the last parameter is the Invocationhandler interface, which is why the dynamic proxy object must implement this interface         *  The resulting proxy object executes the Invoke () method        */        public object newproxy (object targetobject) {            this.targetobject = targetobject;           

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.