Original: https://my.oschina.net/wangnian/blog/801348
Preface: Annotation (Annotated) is introduced in JDK5.0 and later versions, and its function is to annotate other annotations. Now in the development process, everyone has abandoned the traditional way of XML configuration to annotate the way, simple and concise, easy to manage and maintain. The current annotations that refer to third-party jar packages are technical problems, but we also need to use annotations to solve some of the business problems in our work, so we have to get custom annotations.
1. Customizing an annotation
Creating a @interface file means that this is an annotation class
/* * 自定义注解 */@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface SecureValid { String desc() default "身份和安全验证开始...";}
2. Attribute description of the annotation 2.1 @Target
Used to describe the scope of use of annotations (that is, where the annotations described can be used), the values are:
The elementtype.constructor is used to describe the constructor.
The Elementtype.field is used to describe the domain.
Elementtype.local_variable used to describe local variables
Elementtype.method used to describe methods
Elementtype.package used to describe the package
Elementtype.parameter for describing parameters
Elementtype.type used to describe classes or interfaces
2.2 @Retention
Used to describe the life cycle of annotations (that is, the description of the annotations in which they are valid), the values are:
The Retentionpolicy.source is valid in the source file (that is, the source file is reserved).
Retentionpolicy.class valid in class file (that is, class reserved)
Retentionpolicy.runtime valid at run time (that is, runtime retention)
2.3 @Documented
By default, the Javadoc command will not generate our annotation to the doc, so using this tag tells the JDK that it will also generate annotation into Doc.
2.4 @Inherited
For example, there is a class A, there is a tag annotation above him, then the subclass B of a is not to be labeled annotation can inherit
3.Annotation Property value
There are three types: base type, array type, enum type
3.1 Basic String types
public @interface UserdefinedAnnotation { intvalue(); String name() default "zhangsan"; String address(); }使用:@UserdefinedAnnotation(value=123,name="wangwenjun",address="火星") public static void main(String[] args) { System.out.println("hello"); } }
3.2 Arrays
public @interface UserdefinedAnnotation { int[] value(); } 使用: public class UseAnnotation { @UserdefinedAnnotation({123}) public static void main(String[] args) { System.out.println("hello"); } }
3.3 Enum Types
Publicenum dateenum {monday,tuesday,wednesday,thursday,friday,saturday,sunday} and then defines a Annota tion package com.wangwenjun.annatation.userdefined; public @interface userdefinedannotation { dateenum week ();} using: public class useannotation { @UserdefinedAnnotation (week=dateenum.sunday) public static void main (string[] args) {System.out.println (" hello ") ; } }
4. Use SPRINGAOP to enhance
Import Org.aspectj.lang.ProceedingJoinPoint;Import Org.aspectj.lang.annotation.Around;Import Org.aspectj.lang.annotation.Aspect;Import Org.aspectj.lang.annotation.Pointcut;Import Org.slf4j.Logger;Import Org.slf4j.LoggerFactory;Import Org.springframework.core.annotation.Order;Import Org.springframework.http.HttpStatus;Import org.springframework.stereotype.Component;Import Org.springframework.web.context.request.RequestContextHolder;Import org.springframework.web.context.request.ServletRequestAttributes;Import Javax.servlet.http.HttpServletRequest;Import Java.util.Date;@Aspect@ComponentPublicClassAspect {PrivateStaticFinal Logger Logger = Loggerfactory.getlogger (Aspect.class);Pointcut@Pointcut ("@annotation (Annotated Package path)")PublicvoidPointcut() { }@Around ("Pointcut ()")public Object doaround ( Proceedingjoinpoint joinpoint) throws throwable {//GET request HttpServletRequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes ()). GetRequest () ; //block entity class Object target = Joinpoint.gettarget (); //the method name of the Intercept String methodName = Joinpoint.getsignature (). GetName (); //interception method Parameters object[] args = Joinpoint.getargs (); //the type of drop parameter to intercept class[] parametertypes = ((methodsignature) joinpoint.getsignature ()). GetMethod (). Getparametertypes (); //todo processing business code //after processing release object[] args = Joinpoint.getargs (); Span class= "Hljs-keyword" >return joinpoint.proceed (args); } }
Java custom Annotations Springaop