当前位置:网站首页>附加:自定义注解(参数校验注解);(写的不好,别看…)
附加:自定义注解(参数校验注解);(写的不好,别看…)
2022-08-04 15:09:00 【小枯林】
说明:
(1)为什么写了本篇博客?:在【60:第五章:开发admin管理服务:13:开发【新增/修改友情链接,接口】】中,我们自定义了一个@CheckUrl注解,来实现校验url是否OK;
● 所以,本篇博客,就来介绍一下自定义注解的内容;
(2)本篇博客参考的文章有:
●【JAVA元注解@interface详解】,该文的作者是【用户5224393】;
●【Java基础——注解】、【@interface使用详解】,该文的作者是【little-motor】;
●【元注解】,该知乎回答的作者是【eleanor123】;
●【自定义注释@interface的用法理解】,该文的作者是【zhangbeizhen18】;
●【springboot学习(二十二)_ 使用@Constraint注解自定义验证注解】,该文的作者是【Kevin_zheng】;
● 【Spring Boot 之groups应对不同的Validation规则自定义】,该文的作者是【bladestone】;
● 【Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { };】,该文的作者是【笑~笑】;
● 【第 3 章 创建自己的约束规则】;(这篇文章比较重要)
目录
(1)比如@Autowired注解;(该注解使用了【@Target】、【@Documented】、【@Retention】)
(2)比如@Target注解;(发现,元注解也是由元注解解释的;有点套娃的感觉;)(理解可能存在偏差和错误)
(3)比如@RequestBody注解;(该注解使用了【@Target】、【@Documented】、【@Retention】)
四:参考文章4:@Constraint校验注解(javax.validation包下的);
六:【Class[] groups() default {};】、【Class[] payload() default {};】;
一:参考文章1:JAVA元注解@interface详解;
参考自【JAVA元注解@interface详解】,该文的作者是【用户5224393】;
1.参考文章;
即主要是这几点:
(1)借助【@Target】、【@Documented】、【@Retention】、【@Inherited】、【@Native】、【@Repeatable】这些元注解,可以定义其他注解(比如【@Autowired】、【@RequestBody】、【其他自定义注解】等);
(2)【@Target】、【@Documented】、【@Retention】、【@Inherited】、【@Native】、【@Repeatable】这些元注解,是在java.lang.annotation包下定义的;
(3)【@Autowired】、【@RequestBody】、【其他Spring定义的注解】、【其他自定义注解】等注解,需要继承Annotation接口(该接口是java.lang.annotation包下的);
(4)参考自【元注解】,该知乎回答的作者是【eleanor123】;
(5)【我们这儿介绍的元注解】和【"Spring IoC容器与Bean管理24:使用注解方式实现Spring IoC五:元数据注解;"中介绍的元注解】貌似不太是一回事;
● 即【我们这儿介绍的元注解】,更多的是指"解释定义其他注解的,注解";
●【"Spring IoC容器与Bean管理24:使用注解方式实现Spring IoC五:元数据注解;"中介绍的元注解】,更多是Spring定义的、更细化的辅助IoC容器管理对象的,注解;
可以参考【Java基础——注解】,该篇文章比较详细介绍了java注解的内容;
2.个人理解;(理解可能存在偏差和错误)
(1)比如@Autowired注解;(该注解使用了【@Target】、【@Documented】、【@Retention】)
(2)比如@Target注解;(发现,元注解也是由元注解解释的;有点套娃的感觉;)(理解可能存在偏差和错误)
(3)比如@RequestBody注解;(该注解使用了【@Target】、【@Documented】、【@Retention】)
二:参考文章2:@interface使用详解;
参考自【@interface使用详解】,该文的作者是【little-motor】;
三:参考文章3:自定义注释@interface的用法理解;
参考自【自定义注释@interface的用法理解】,该文的作者是【zhangbeizhen18】;
<5>.测试自定义注解
public class TestMain { public static void main(String[] args) throws Exception { Class cls = Class.forName("com.zbz.annotation.pattern3.Worker"); Method[] method = cls.getMethods(); /**判断Worker类上是否有TypeAnnotation注解*/ boolean flag = cls.isAnnotationPresent(TypeAnnotation.class); /**获取Worker类上是TypeAnnotation注解值*/ if (flag) { TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class); System.out.println("@TypeAnnotation值:" + typeAnno.value()); } /**方法上注解*/ List<Method> list = new ArrayList<Method>(); for (int i = 0; i < method.length; i++) { list.add(method[i]); } for (Method m : list) { MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class); if (methodAnno == null) continue; System.out.println( "方法名称:" + m.getName()); System.out.println("方法上注解name = " + methodAnno.name()); System.out.println("方法上注解url = " + methodAnno.url()); } /**属性上注解*/ List<Field> fieldList = new ArrayList<Field>(); for (Field f : cls.getDeclaredFields()) {// 访问所有字段 FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class); System.out.println( "属性名称:" + f.getName()); System.out.println("属性注解值FiledAnnotation = " + filedAno.value()); } } }
四:参考文章4:@Constraint校验注解(javax.validation包下的);
参考自【springboot学习(二十二)_ 使用@Constraint注解自定义验证注解】,该文的作者是【Kevin_zheng】;
……
五:自己项目中的@CheckUrl注解;
还勉强能够理解一下哈;
……………………………………………………
只是上面的【Class<?>[] groups() default {};】、【Class<? extends Payload>[] payload() default {};】;还不太理解;
六:【Class<?>[] groups() default {};】、【Class<? extends Payload>[] payload() default {};】;
可以参考下:
● 【Spring Boot 之groups应对不同的Validation规则自定义】,该文的作者是【bladestone】;
● 【Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { };】,该文的作者是【笑~笑】;
● 【第 3 章 创建自己的约束规则】;(这篇文章比较重要)
边栏推荐
猜你喜欢

IP第十六天笔记

杭电校赛(逆袭指数)

明明加了唯一索引,为什么还是产生重复数据?

Hangzhou Electric School Competition (Counter Attack Index)

如何优雅的消除系统重复代码?
![[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution](/img/1c/3c8c323e6ee3406d202e07f85bab21.jpg)
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution

技术分享| 小程序实现音视频通话

关于pnpm包管理器的版本问题

leetcode: 255 Verify preorder traversal sequence binary search tree

手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
随机推荐
leetcode: 255 Verify preorder traversal sequence binary search tree
16、学习MySQL 正则表达式
Roslyn 通过 nuget 统一管理信息
SublimeText 粘贴图片保存到本地
洛谷题解P1028 数的计算
Leetcode: 215 disorderly to find the first big k element in the array
IP第十六天笔记
QT笔记——Q_INVOKABLE了解
Semaphore 基本原理
1401 - Web technology 】 【 introduction to graphical Canvas
实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
C# SolidWorks二次开发---工程图简单版标注孔信息
技术分享| 小程序实现音视频通话
我爱七夕哈哈哈
动态数组底层是如何实现的
宣传海报
C# TextBlock 上标
IP报文头解析
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
leetcode:241. 为运算表达式设计优先级

























