看几个基础的注解
@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开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
这个月马上就又要过去了,还在找工作的小伙伴要做好准备了,小编整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
zoom: 33%;" />
总结
这个月马上就又要过去了,还在找工作的小伙伴要做好准备了,小编整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家
[外链图片转存中…(img-VJtVQDWK-1713446509952)]
[外链图片转存中…(img-R5h4qvjE-1713446509952)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!