+ Custom annotation logging aspect-oriented integration module (a)

java in common comment

jdk's own notes

  • @Override: I tell the compiler to rewrite the interface method
  • @Deprecated: tell the compiler that this method obsolete, is not recommended, Ide will draw a line through the method
  • Warning Close method appears: @SuppressWarnings ( "deprecation")

Yuan notes

Meta-annotation notes that the role of other comments, we generally use custom annotations, you need to use meta-annotation? To mark our own notes, a total of four yuan notes

1. @ Target: modified Annotation described range, may be used for packages, types (classes, interfaces, enumerations, Annotation type), a member type (method, constructor, member variables enumerated value), method parameters and local variables (e.g., variable loop, catch parameters). Use a target in the Annotation type declaration can be more clear that modified target

例:@Target(ElementType.TYPE)
1.ElementType.CONSTRUCTOR:用于描述构造器
2.ElementType.FIELD:用于描述域(类的成员变量)
3.ElementType.LOCAL_VARIABLE:用于描述局部变量(方法内部变量)
4.ElementType.METHOD:用于描述方法
5.ElementType.PACKAGE:用于描述包 6.ElementType.PARAMETER:用于描述参数 7.ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明 

2. @ Retention: defines the length of time the Annotation reserved, leaving only some of the source code, compiled into a class is sometimes necessary to retain, when running some programs require reservations. Which describes the life cycle of notes

例:@Retention(RetentionPolicy.RUNTIME)
1.RetentionPoicy.SOURCE:在源文件中有效(即源文件保留)
2.RetentionPoicy.CLASS:在class文件中有效(即class保留) 3.RetentionPoicy.RUNTIME:在运行时有效(即运行时保留) 

3. @ Documented: it is a marker annotation, that is not a member of notes, used to describe other types of annotation should be as a member of the public API program is marked, it can be such a tool such as javadoc documentation

4. @ Inherited: after it is also an annotation tag, its role is marked by its type is inherited, such as a class is @Inherited tagged, a subclass inherits the class, then this annotation is used in a subclass of this class.

Note: After a type of modified @Inherited, not class, not inherited annotation method from overloaded method it inherits from the interface it implements annotation.

Custom annotation

Custom annotation format:

public @interface annotation definition body name} {

Use @interface defined? An annotation that automatically inherited java.lang.annotation.Annotation interfaces, each of which method is actually declares a configuration parameter. Name of the method is the name of the parameter, return type is the type parameter (return value type can only be a basic type, Class, String, enum). You can declare the default value of the parameter by default.

Notes parameter can support data types:

1.所有基本数据类型(int,float,boolean,byte,double,char,long,short) 2.String类型 3.Class类型 4.enum类型 5.Annotation类型 6.以上所有类型的数组 

Members note that the definition of annotation points: first, only with public or default (default) access to these two modifications, such as, String value (); here, the default method is set defaul type;. 

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface userName { String value() default ""; } 

  Second, the basic parameters members can only use the type byte, short, char, int, long, float, double, boolean eight basic data types and String, Enum, Class, annotations and other data types, as well as some types of this array.

//定义一个枚举
public enum RequestMethod {
	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping { String name() default ""; String[] path() default {}; RequestMethod[] method() default {};//枚举数组 } 

Third, if a member of only one parameter, it is best to set the parameter name "value", after adding parentheses.

Notes default

Notes element must have a definite value? Or to specify a default value, or the value to use. But sometimes we need to determine the value of the expression of an element does not exist, it is an empty string or a negative number indicates an element does not exist, when defining notes, it has become a convention usage.

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface User { public int id() default -1; public String name() default ""; public String address() default ""; } 

For supplementary annotation @Inherited

Enter Caption

Conclusion: the class of the parent class and the methods are custom annotations, and is @Inherited mark, then the parent will inherit notes the case of a subclass inherits only. Rewrite, overloading, parent implementation of these will not inherit the parent class notes.

Article link: https://my.oschina.net/itgaowei/blog/1602277

Guess you like

Origin www.cnblogs.com/danceIt/p/11301310.html