Define attributes/methods for annotations and use and control transactions for reflection (66), annotation 66

Source: Internet
Author: User

Define attributes/methods for annotations and use and control transactions for reflection (66), annotation 66
8. Define attributes/methods for annotations

If an annotation requires an identifier, define an attribute for it..

METHOD defined: @ Retention (RetentionPolicy. RUNTIME) @ Target (value = {ElementType. METHOD}) public @ interface MyTest {/*** for one-person annotation class. * The value attribute is the officially recommended name * and the value is also the default attribute * attributes defined below, because there is no default value, therefore, the user * must give an explicit value */public String value () during use;/*** defines an attribute with default values */public String name () default "NoName ";}

Get the property value on the annotation:

 

<A extends Annotation>
A

 

getAnnotation(Class<A> annotationClass)
If a comment of the specified type of the element exists, the comments are returned. Otherwise, null is returned.

Public class AnlyValueDemo {public static void main (String [] args) throws Exception {RunTest run = new RunTest (); Method m1 = run. getClass (). getMethod ("bbb"); // instance for obtaining the Annotation on this method MyTest mt = m1.getAnnotation (MyTest. class); // obtain the attribute value on this annotation String name = mt. name (); String value = mt. value (); System. err. println (name + "," + value );}}

Purpose: Generate and save the table structure. The following isSUNNotes provided by the company:

There are many annotations in the system, some of which areJDBC.

@ Table-Define a class, which corresponds to a table-Domain Model

@ Column-Define a field in a class

@ Id-Define primary keys

@ OnToMany

@ OneToOne

@ Manytoyun

9. annotation for reflection and transaction control

For allServiceProxy.

Requirements:

An interface must be owned by the proxy class.

The proxy has two core classes:

Proxy:Generate a subclass of the interface in the memory.

InvocationHandler:Execution handleWhen executing the proxy class methodInvocationhandlerIt intercepts all proxy methods.

 

Example:

RequiredListProxy:

Package cn. hx. proxy; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. util. arrayList; import java. util. list; public class ProxyDemo {public static void main (String [] args) throws Exception {// declare the proxy class final List list = new ArrayList (); // generate Proxy class Object obj = Proxy. newProxyInstance (ProxyDemo. class. getClassLoader (), new Class [] {List. class}, new InvocationHandler () {public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. err. println ("executed a method:" + method. getName (); // The execution is proxy class Object returnValue = method. invoke (list, args); return returnValue ;}}); // convert the proxy class to the interface Object List list2 = (List) obj; list2.add ("ddd"); System. err. println (list2.get (0 ));}}

It is required that a class can be used as a proxy for all classes with Interfaces:

Package cn. hx. proxy; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy;/*** this class is not only a tool class, but also an execution handle **/public class MyProxy2 implements InvocationHandler {/*** declares that it is the Proxy class */private Object src; the/*** constructor receives the proxy Object */private MyProxy2 (Object src) {this. src = src;}/*** provides a static method to return Proxy objects */public static Object factory (Object src) {Object proxyedObj = Proxy. newProxyInstance (MyProxy2.class. getClassLoader (), src. getClass (). getInterfaces (), new MyProxy2 (src); return proxyedObj;}/*** implement the Interception Method */public Object invoke (Object proxy, method Method, Object [] args) throws Throwable {System. err. println ("execution method >>>:+" + method. getName (); Object rVlaue = method. invoke (src, args); return rVlaue ;}}

 

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.