Annotation
I want to learn is by Spring @bean
,Want to know how it work.
But toke a long time in busy.
Until today,There is my notes about Annotation.
First you need create class and replace class
to @interface
All Annotation implicit inheritance from java.lang.annotation.Annotation
And all your annotation can use meta-annotation
There have four meta-annotation
@Target
mark where your annotation can use, without@Target
, your annotation can use everywhere- ElementType.ANNOTATION_TYPE —mark annotation type
- ElementType.PACKAGE —above package
- ElementType.TYPE —above class and interface
- ElementType.METHOD — above method
- ElementType.CONSTRUCT —above construct
- ElementType.FIELD —member domain
- ElementType.PARAMETER —above method or whit parameter
- ElementType.LOCAL_VARIABLE —local variable
- TElementType.YPE_PARAMETER —type parameter
- ElementType.TYPE_USE —type use
@Retention
be used to mark a annotation keep time, without@Retention
, default is RetentionPolicy.CLASS- RetentionPolicy.SOURCE —not include class file
- RetentionPolicy.CLASS —include class file but
JVM
not load - RetentionPolicy.RUNTIME —include class file and
JVM
will load, you can get them by reflex
@Document
create note for archive like JavaDoc@Inherited
only be used to class, if a has extend annotation, then it all child class auto have the same annotation
ok, there is a demo.
first need create a annotation by yourself.
The meta-annotation above public @interface Foo {}
you can chose which is necessary1
2
3
4
5
6
7
8import java.lang.annotation.*;
(RetentionPolicy.RUNTIME)
public Foo {
String name() default "";
int count() default 0;
}
this annotation Foo
has two value, name and count.
that value type can be
- Base-Type (int, short, long, byte, char, double, float or boolean)
- String
- Class (such as Class<? extend Bar>)
- Enum
- Annotation
- Array (create from above type)
so this Foo
can be use now
1 | "这是类注解",count = 1) (value = |
as you can see @Foo
can use above class, property and method.
because of when i create it whit @Target
, so Foo
can use whit them.
but only annotation can not do anything.
must be have a “process” to handle.
so there is my “process”:
1 | public class FooProcess{ |
and run with :1
2
3
4
5
6
7public static void main(String args[]){
try {
FooProcess.analyze(Bar.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
see console1
2
3
4
5
6
7
8
9Bar class has Foo annotation
info: this is class annotation,1
Bar class annotation is:interface com.eddie.annotation.Foo
FiledName[name]property annotation: interface com.eddie.annotation.Foo
FiledName[name]property info: this is property annotation,2
test method has annotation type: interface com.eddie.annotation.Foo
info: this is method annotation,3
you can do everything in process, but first you need use if to know target location
real have your annotation, if not, you will get a NullPointerException or maybe
ClassCastException. we don’t want to see them, right?
then you can get value and do what you want.