Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize annotations in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to customize annotations in Java. The quality of the article is high, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.

Java basics, annotations and custom annotations

Java annotation is an annotation mechanism introduced by JDK 5.0.

I. Self-explanatory

Before we learn about custom annotations, let's look at Java's internally defined set of annotations: there are seven of them, three in java.lang, and four in java. lang. annotation.

Acting on a class or method:

@Override Checks if the method is an override method. Compile errors are reported if the method is not found in its parent class, or in the referenced interface.@ Deprecated tag obsolete method. It is not recommended to use @SuppressWarnings to instruct the compiler to ignore warnings declared in annotations

Works on other comments

@Retention identifies how the annotation is saved, whether it is only in code, codified in a class file, or accessible via reflection at runtime.@ Documented flag Whether these annotations are included in the user documentation @Target flag What Java member should this annotation be @Inherited flag What annotation class does this annotation inherit from (default annotation does not inherit from any subclass)

Three additional comments added:

@SafeVarargs Java 7 supports ignoring any warnings generated by method or constructor calls that use generic variables as arguments.@ FunctionalInterface Java 8 supports identifying an anonymous function or functional interface.@ Repeatable Java 8 began to support, indicating that an annotation can be used multiple times on the same declaration. II. Annotations Structure

Annotation有三个重要的主干类,分别为:Annotation、ElememtType、RetentionPolicy

1、Annotation:public interface Annotation { boolean equals(Object obj); int hashCode(); String toString(); Class paramsMap = (Map) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000)); } } /** * 是否存在注解,如果存在就获取 */ private Log getAnnotationLog(JoinPoint joinPoint) throws Exception { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) { return method.getAnnotation(Log.class); } return null; } /** * 参数拼装 */ private String argsArrayToString(Object[] paramsArray) { String params = ""; if (paramsArray != null && paramsArray.length > 0) { for (int i = 0; i < paramsArray.length; i++) { if (!isFilterObject(paramsArray[i])) { Object jsonObj = JSON.toJSON(paramsArray[i]); params += jsonObj.toString() + " "; } } } return params.trim(); } /** * 判断是否需要过滤的对象。 * * @param o 对象信息。 * @return 如果是需要过滤的对象,则返回true;否则返回false。 */ public boolean isFilterObject(final Object o) { return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse; }}3.4、注解的使用/** * 修改推荐位 */@Log(title = "推荐位", businessType = BusinessType.UPDATE)@PutMapping("/edit")public AjaxResult edit(@RequestBody GsRecommPosition gsRecommPosition){ Long gsRecommPositionId = gsRecommPosition.getGsRecommPositionId(); boolean condition = recommPositionHasCategory(gsRecommPositionId); return toAjax(gsRecommPositionService.updateGsRecommPosition(gsRecommPosition));}/** * 删除推荐位 */@Log(title = "推荐位", businessType = BusinessType.DELETE)@DeleteMapping("delete/{gsRecommPositionId}")public AjaxResult remove(@PathVariable Long gsRecommPositionId){ boolean condition = recommPositionHasCategory(gsRecommPositionId); return toAjax(gsRecommPositionService.deleteGsRecommPositionById(gsRecommPositionId));}四、JoinPoint解析1、JoinPoint

对象封装了SpringAop切面方法中的信息

方法名功能Signature getSignature();获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息Object[] getArgs();获取传入目标方法的参数对象Object getTarget();获取被代理的对象Object getThis();获取代理对象joinPoint.getSignature().getName(); // 获取目标方法名joinPoint.getSignature().getDeclaringType().getSimpleName(); // 获取目标方法所属类的简单类名joinPoint.getSignature().getDeclaringTypeName(); // 获取目标方法所属类的类名joinPoint.getSignature().getModifiers(); // 获取目标方法声明类型(public、private、protected)Object[] args = joinPoint.getArgs(); // 获取传入目标方法的参数,返回一个数组joinPoint.getTarget(); // 获取被代理的对象joinPoint.getThis(); // 获取代理对象自己//获取自定义注解的信息MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();Method method = methodSignature.getMethod();CustomLog customLog = methodSignature.getMethod().getAnnotation(CustomLog.class);2、ProceedingJoinPoint

ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中

Object proceed() throws Throwable //执行目标方法 Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法关于Java中怎么自定义注解就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report