原文:https://my.oschina.net/wangnian/blog/801348
前言:Annotation(注解)是JDK5.0及以后版本引入的,它的作用就是負責注解其他注解。現在開發過程中大家都已經放棄了傳統的XML配置的方式改為注解的方式,既簡單又簡潔,方便管理和維護。目前引用第三方jar包的注解都是解決技術上的問題,然而我們在工作中也需要通過注解解決業務上的一些問題,所以就得用到自定義注解。
1.自定義一個注解
創建一個 @interface的文件,則表示這是一個注解類
/* * 自定義注解 */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface SecureValid { String desc() default "身份和安全驗證開始..."; }
2.注解的屬性描述
2.1 @Target
用於描述注解的使用范圍(即:被描述的注解可以用在什么地方),其取值有:
ElementType.CONSTRUCTOR 用於描述構造器。
ElementType.FIELD 用於描述域。
ElementType.LOCAL_VARIABLE 用於描述局部變量
ElementType.METHOD 用於描述方法
ElementType.PACKAGE 用於描述包
ElementType.PARAMETER 用於描述參數
ElementType.TYPE 用於描述類或接口
2.2 @Retention
用於描述注解的生命周期(即:被描述的注解在什么范圍內有效),其取值有:
RetentionPolicy.SOURCE 在源文件中有效(即源文件保留)。
RetentionPolicy.CLASS 在 class 文件中有效(即 class 保留)
RetentionPolicy.RUNTIME 在運行時有效(即運行時保留)
2.3 @Documented
在默認的情況下javadoc命令不會將我們的annotation生成再doc中去的,所以使用該標記就是告訴jdk讓它也將annotation生成到doc中去
2.4 @Inherited
比如有一個類A,在他上面有一個標記annotation,那么A的子類B是否不用再次標記annotation就可以繼承得到
3.Annotation屬性值
有以下三種: 基本類型、數組類型、枚舉類型
3.1 基本串類型
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 數組
public @interface UserdefinedAnnotation { int[] value(); } 使用: public class UseAnnotation { @UserdefinedAnnotation({123}) public static void main(String[] args) { System.out.println("hello"); } }
3.3 枚舉類型
public enum DateEnum { Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday } 然后在定義一個annotation package com.wangwenjun.annatation.userdefined; public @interface UserdefinedAnnotation { DateEnum week(); } 使用: public class UseAnnotation { @UserdefinedAnnotation(week=DateEnum.Sunday) public static void main(String[] args) { System.out.println("hello"); } }
4.使用SpringAOP來增強
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 @Component public class Aspect { private static final Logger logger = LoggerFactory.getLogger(Aspect.class); //切入點 @Pointcut("@annotation(注解的包路徑)") public void pointcut() { } @Around("pointcut()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { //獲取request HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); //攔截的實體類 Object target = joinPoint.getTarget(); //攔截的方法名稱 String methodName = joinPoint.getSignature().getName(); //攔截的方法參數 Object[] args = joinPoint.getArgs(); //攔截的放參數類型 Class[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes(); //TODO 處理業務代碼 //處理完之后放行 Object[] args = joinPoint.getArgs(); return joinPoint.proceed(args); } }