關於注解的定義,使用等就不說了,在這里直接上干貨,自定義注解相關的東西。
元注解的作用就是注解其他注解,一般我們使用自定義注解時,就需要用元注解來標注我們自己的注解,一共有四個元注解
元注解:
java.lang.annotation提供了四種元注解,專門注解其他的注解(在自定義注解的時候,需要使用到元注解):
@Documented –注解是否將包含在JavaDoc中
@Retention –什么時候使用該注解
@Target –注解用於什么地方
@Inherited – 是否允許子類繼承該注解
1.)@Retention– 定義該注解的生命周期
● RetentionPolicy.SOURCE : 在編譯階段丟棄。這些注解在編譯結束之后就不再有任何意義,所以它們不會寫入字節碼。@Override, @SuppressWarnings都屬於這類注解。
● RetentionPolicy.CLASS : 在類加載的時候丟棄。在字節碼文件的處理中有用。注解默認使用這種方式
● RetentionPolicy.RUNTIME : 始終不會丟棄,運行期也保留該注解,因此可以使用反射機制讀取該注解的信息。我們自定義的注解通常使用這種方式。
2.)Target – 表示該注解用於什么地方。默認值為任何元素,表示該注解用於什么地方。可用的ElementType參數包括
● ElementType.CONSTRUCTOR:用於描述構造器
● ElementType.FIELD:成員變量、對象、屬性(包括enum實例)
● ElementType.LOCAL_VARIABLE:用於描述局部變量
● ElementType.METHOD:用於描述方法
● ElementType.PACKAGE:用於描述包
● ElementType.PARAMETER:用於描述參數
● ElementType.TYPE:用於描述類、接口(包括注解類型) 或enum聲明
3.)@Documented–一個簡單的Annotations標記注解,表示是否將注解信息添加在java文檔中。
4.)@Inherited – 定義該注釋和子類的關系
@Inherited 元注解是一個標記注解,@Inherited闡述了某個被標注的類型是被繼承的。如果一個使用了@Inherited修飾的annotation類型被用於一個class,則這個annotation將被用於該class的子類。
自定義注解格式:
public @interface 注解名 {定義體}
1. Annotation型定義為@interface, 所有的Annotation會自動繼承java.lang.Annotation這一接口,並且不能再去繼承別的類或是接口.
2. 參數成員只能用public或默認(default)這兩個訪問權修飾
3. 參數成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數據類型和String、Enum、Class、annotations等數據類型,以及這一些類型的數組.
4. 要獲取類方法和字段的注解信息,必須通過Java的反射技術來獲取 Annotation對象,因為你除此之外沒有別的獲取注解對象的方法
5. 注解也可以沒有定義成員,
因業務需要,為了放置表單數據重復提交,比如轉賬,誤操作點了兩下,這時候給出一個操作頻繁的提示,那么我們可以寫一個注解,在需要防止重復提交的地方添加上注解就ok了。代碼如下:
自定義一個注解
1 @Target(ElementType.METHOD) 2 @Retention(RetentionPolicy.RUNTIME) 3 @Documented 4 public @interface AvoidRepeatableCommit { 5 6 long timeout() default 5; 7 }
切面:
1 @Aspect 2 @Component 3 public class VoidRepeatCommitAspectJ { 4 5 Logger logger = LoggerFactory.getLogger(this.getClass()); 6 @Autowired 7 @Qualifier("redisTemplate2") 8 private RedisTemplate<String, Object> template; 9 10 @Pointcut("@annotation(com.topband.beings.aop.AvoidRepeatableCommit)") 11 public void cutPoint(){} 12 @Around("cutPoint()") 13 public Object around(ProceedingJoinPoint point) throws Throwable{ 14 logger.info("around point : {}",point); 15 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); 16 String ip = ServletUtils.getIpAddr(); 17 String token = request.getHeader(SystemConstants.TOKEN_HEADER); 18 logger.info("ip:{};token:{}",ip,token); 19 //獲取注解 20 MethodSignature signature = (MethodSignature) point.getSignature(); 21 Method method = signature.getMethod(); 22 //目標類、方法 23 String className = method.getDeclaringClass().getName(); 24 String methodName = method.getName(); 25 AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit.class); 26 long timeout = avoidRepeatableCommit.timeout(); 27 if (timeout < 0){ 28 timeout = 5; 29 } 30 String key =SystemConstants.TOKEN_CACHE_PREFIX + token + "-" + className + "-" + methodName; 31 logger.info("key:{}; template:{}",key,template); 32 Object obj = template.opsForValue().get(key); 33 logger.info("obj:{}",obj); 34 String str = null; 35 if(obj==null){ 36 ; 37 }else{ 38 str = (String) template.opsForValue().get(key); 39 logger.info("str:{}",str); 40 } 41 // String str = redisUtil.get(key); 42 //查詢redis 中是否存在此key 無則添加,有則拋出異常 43 if (StringUtil.isEmpty(str)){ 44 String value = token + "-" + className + "-" + methodName; 45 template.opsForValue().set(key, value, 5,TimeUnit.SECONDS); 46 return point.proceed(); 47 }else { 48 ResponseObj response = new ResponseObj(); 49 response.setStatus(Defined.STATUS_ERROR); 50 response.setMessage("操作過於頻繁"); 51 return response; 52 } 53 } 54 }
然后在需要防止重復提交的地方,加上注解就可以了。