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:
|
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 ;}}