The basic implementations of the frameworks in Java, whether AOP or IoC, are derived from the reflection capabilities of the Java Runtime support.
And the most basic point of reflection is that any class in the JVM environment, there is an object class object, this object provides some aspects of the class method constructor annotation, etc.
Here is a small example of reflection and annotations, a relatively good introduction to the small demo
After understanding, almost all of the frameworks are implemented in accordance with this idea.
Packagepw.jonwinters.annotation;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Inherited;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;Importjava.lang.annotation.Target; @Documented//generate Javadoc will contain annotation information@Inherited//allow child class annotations to inherit@Target ({elementtype.method,elementtype.constructor,elementtype.type})//Note the position of the annotation method is the annotation on the constructor the annotation type on the constructor is for annotations on type and interface, etc.@Retention (Retentionpolicy.runtime)//The life cycle of the annotations, where the runtime is noted, meaning that this annotation is still present when the annotation is translated into a class file while it is running on the virtual machine. Public@InterfaceDescription {String desc ()default"Winter";//default is winter} if no value is assigned by default
Packagepw.jonwinters.annotation;ImportJava.lang.reflect.Method;Importorg.junit.Test;Import Staticorg.junit.Assert.assertNotNull; @Description Public classMansImplementsPerson {@Override @Description Public voidrun () {//TODO auto-generated Method StubSystem.out.println ("I am Run () method!"); } @Test Public voidTestannotation ()throwsclassnotfoundexception, instantiationexception, illegalaccessexception{Class clazz= Class.forName ("Pw.jonwinters.annotation.Man")///First load Our man class Assertnotnull via Class.forName ("Can ' t find class", Clazz), or//can not find the prompt method[] methods=clazz.getdeclaredmethods ();//After getting Clazz object, get all the methods of the man class for(Method method:methods) {if(Method.isannotationpresent (Description.class) {//For each method to determine whether it exists description annotation System.out.println ("Method:" +method.getname () + "() has Description annotation!"); System.out.println (Method.getannotation (Description.class). Desc ());//The DESC field for printed annotations is winter} by defaultif(Method.isannotationpresent (Override).class)) System.out.println ("Method:" +method.getname () + "has Override annotation!"); The Overrider annotation is that Retentionpolicy.source only exists for the source code and does not exist when the JVM is running, so we cannot find the override annotation above the run () method.}if(Clazz.isannotationpresent (Description.class) {(man) clazz.newinstance ()). Run ();//Finally, if the class also has description annotations, instantiate a Clazz object down to the man method and call its Run method to print I am Run Method} }}
Annotations and reflections---the implementation principles of the framework such as spring and MyBatis