Java Annotations and proxy implementations

Source: Internet
Author: User

1. Define Annotations

Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target, @Documented @retention (retentionpolicy.runtime) @Target (Elementtype.method) Public@Interfacemethodannotation {String name ()default"Default Annotation Name"; String URL ()default"Default.com";}

2. Information class using annotations

 Public Interface iproxytest {    @MethodAnnotation ()    publicvoid  testmethodannotation ();        @MethodAnnotation (Name= "Amazon", url= "z.cn")    publicvoid  Testmethodannotationwithvalue ();}

3. Implement the interface

 Public class Implements iproxytest {    publicvoid  testmethodannotation () {        System.out.println (" Useannotion->testmethodannotation ");    }          Public void Testmethodannotationwithvalue () {        System.out.println ("useannotion-> Testmethodannotationwithvalue ");}    }

4. Dynamic proxy class implementation and main test

ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy; Public classLogproxyImplementsInvocationhandler {Private Static intCount = 0; PrivateObject Target; PrivateLogproxy () {}Private StaticObject getinstance (Object o) {logproxy LP=NewLogproxy (); Lp.target=o; Object result=proxy.newproxyinstance (O.getclass (). getClassLoader (), O. GetClass (). Getinterfaces (), LP);//Proxy Object        returnresult; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {if(Method.isannotationpresent (methodannotation.class) {methodannotation annotation=method. getannotation (methodannotation.class); Count++;//Usage Scenario: Count the number of interfaces that can be degraded within the performance range System.out.println ("Execute Method" + method.getname () + ":" +count); } Object obj=Method.invoke (target, args); returnobj; }     Public Static voidMain (string[] args) {iproxytest ua=NewProxytestimpl (); Iproxytest LP=(iproxytest) logproxy.getinstance (UA);        Lp.testmethodannotation ();    Lp.testmethodannotationwithvalue (); }}

Java Annotations and proxy implementations

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.