不要眨眼!这次从Spring源码中学习带你查找自定义注解

不要眨眼!这次从Spring源码中学习带你查找自定义注解

看几个基础的注解

@AliasFor

=========

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

@Documented

public @interface AliasFor {

@AliasFor(“attribute”)

String value() default “”;

@AliasFor(“value”)

String attribute() default “”;

Class<? extends Annotation> annotation() default Annotation.class;

}

AliasFor这个注解很奇怪,value的别名是attribute,attribute的别名是value

那么它的行为在哪里被定义的呢?在AnnotationTypeMapping中我们可以找到答案

// 这里使用了AnnotationsScanner的getDeclaredAnnotation方法来获取所有的AliasFor注解的方法

// AnnotationsScanner 是spring中的非公开抽象类,在我们的代码中不能直接进行使用

// Spring中没有提供子类

private Map<Method, List> resolveAliasedForTargets() {

Map<Method, List> aliasedBy = new HashMap<>();

for (int i = 0; i < this.attributes.size(); i++) {

Method attribute = this.attributes.get(i);

AliasFor aliasFor = AnnotationsScanner.getDeclaredAnnotation(attribute, AliasFor.class);

if (aliasFor != null) {

Method target = resolveAliasTarget(attribute, aliasFor);

aliasedBy.computeIfAbsent(target, key -> new ArrayList<>()).add(attribute);

}

}

return Collections.unmodifiableMap(aliasedBy);

}

// 为了简洁,我将源代码中其余部分省略掉了,可以看到,这里使用用反射得到的Method的getAnnotation方法得到实例

private Method resolveAliasTarget(Method attribute, AliasFor aliasFor, boolean checkAliasPair) {

// … …

Method target = AttributeMethods.forAnnotationType(targetAnnotation).get(targetAttributeName);

// … …

if (isAliasPair(target) && checkAliasPair) {

AliasFor targetAliasFor = target.getAnnotation(AliasFor.class);

if (targetAliasFor != null) {

Method mirror = resolveAliasTarget(target, targetAliasFor, false);

if (!mirror.equals(attribute)) {

throw new AnnotationConfigurationException(String.format(

“%s must be declared as an @AliasFor %s, not %s.”,

StringUtils.capitalize(AttributeMethods.describe(target)),

AttributeMethods.describe(attribute), AttributeMethods.describe(mirror)));

}

}

}

return target;

}

通过学习@AliasFor,我们知道了可以通过先活动Method,再或得其修饰的注解的方法。

根据这样的方法,我们可以使用下面的代码,找到类DockingHandlers中所有被注解@DockIngMessage修饰的方法

// DockIngMessage 是自定义的注解

Method[] methods = DockingHandlers.class.getMethods();

for (Method method : methods) {

DockIngMessage dockIngMessage = method.getAnnotation(DockIngMessage.class);

if (dockIngMessage != null) {

System.out.println(dockIngMessage.name());

}

}

@Bean

=====

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Bean {

@AliasFor(“name”)

String[] value() default {};

@AliasFor(“value”)

String[] name() default {};

@Deprecated

Autowire autowire() default Autowire.NO;

boolean autowireCandidate() default true;

String initMethod() default “”;

String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

}

@Bean注解是Spring中用得比较广泛的注解之一,来看看Spring源码中是怎么查找@Bean注解的

public static boolean isBeanAnnotated(Method method) {

return AnnotatedElementUtils.hasAnnotation(method, Bean.class);

}

使用了AnnotatedElementUtils工具类,那么我们就可以把上面的代码改造一下

Method[] methods = DockingHandlers.class.getMethods();

for (Method method : methods) {

if (AnnotatedElementUtils.hasAnnotation(method, DockIngMessage.class)) {

DockIngMessage dockIngMessage = AnnotatedElementUtils.getMergedAnnotation(method,DockIngMessage.class);

System.out.println(dockIngMessage.name());

}

}

// 相比于判断 != null , 这样的写法相对优雅了许多

至于Bean到底是怎么生效的,我们需要留到以后研究Spring容器的时候再讨论

@Controller

===========

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Component

public @interface Controller {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

总结

这个月马上就又要过去了,还在找工作的小伙伴要做好准备了,小编整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家

在这里插入图片描述

在这里插入图片描述

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
zoom: 33%;" />

总结

这个月马上就又要过去了,还在找工作的小伙伴要做好准备了,小编整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家

[外链图片转存中…(img-VJtVQDWK-1713446509952)]

[外链图片转存中…(img-R5h4qvjE-1713446509952)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架允许我们自定义注解,用于标记特定的类、方法或属性,并通过反射来获取或处理这些注解。下面是创建自定义注解的步骤: 1. 创建一个新的Java类,用于定义自定义注解。注解需要使用`@interface`关键字进行声明。例如: ```java package com.example.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface CustomAnnotation { // 在这里定义注解的属性 String value() default ""; } ``` 在上面的例子,我们创建了一个名为`CustomAnnotation`的自定义注解,该注解被定义为在运行时可见,并且可以应用于类级别。 2. 在类使用自定义注解。例如: ```java package com.example; import com.example.annotations.CustomAnnotation; @CustomAnnotation("Hello, world!") public class MyClass { // 类的具体实现 } ``` 在上面的例子,我们在`MyClass`类上使用了自定义注解`CustomAnnotation`,并传递了一个字符串参数作为注解的值。 3. 使用反射获取自定义注解信息。例如: ```java package com.example; import com.example.annotations.CustomAnnotation; public class Main { public static void main(String[] args) { Class<?> clazz = MyClass.class; CustomAnnotation annotation = clazz.getAnnotation(CustomAnnotation.class); if (annotation != null) { String value = annotation.value(); System.out.println("Annotation value: " + value); } } } ``` 在上面的例子,我们使用反射获取`MyClass`类上的`CustomAnnotation`注解,并获取注解的值。 这就是使用Spring框架自定义注解的基本步骤。你可以根据实际需求在注解定义不同的属性,并根据需要在应用程序使用这些自定义注解

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值