日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring注解编程基石(三)

發布時間:2024/4/13 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring注解编程基石(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

AnnotationUtils 源碼分析

方法列表

方法源碼

AnnotatedElementUtils 源碼分析


Spring注解編程基石(一)

Spring注解編程基石(二)

Spring注解編程基石(三)

Spring注解編程基石(四)


AnnotationUtils 源碼分析

方法列表

//從指定annotation 上查找單個指定annotationType的注解。返回值為它本身或者直接元注解。 public static <A extends Annotation> A getAnnotation(Annotation annotation, Class<A> annotationType); //從指定annotatedElement 上查找單個指定annotationType的注解。返回值為應用在annotatedElement上的注解或者注解的第一級元注解。 public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType); //從指定method 上查找單個指定annotationType的注解。返回值為應用在method上的注解或者注解的第一級元注解。 public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType);//從指定class 上查找單個指定annotationType的注解。查找遍歷它的注解,實現接口,superclass。 public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType); //從指定annotatedElement 上查找單個指定annotationType的注解。返回值為應用在annotatedElement上的注解或者元注解。 public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, @Nullable Class<A> annotationType); //從指定method 上查找單個指定annotationType的注解。返回值為應用在method上的注解或者超類(或者接口)上的overrided的方法的注解。 public static <A extends Annotation> A findAnnotation(Method method, @Nullable Class<A> annotationType);

get***方法源碼

public static <A extends Annotation> A getAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) {// 如果是java包下注解獲取聲明的注解。if (AnnotationFilter.PLAIN.matches(annotationType) ||AnnotationsScanner.hasPlainJavaAnnotationsOnly(annotatedElement)) {return annotatedElement.getAnnotation(annotationType);}// Exhaustive retrieval of merged annotations...return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none()).get(annotationType).withNonMergedAttributes().synthesize(AnnotationUtils::isSingleLevelPresent).orElse(null);}public static <A extends Annotation> A getAnnotation(Annotation annotation, Class<A> annotationType) {// Shortcut: directly present on the element, with no merging needed?if (annotationType.isInstance(annotation)) {return synthesizeAnnotation((A) annotation, annotationType);}// Shortcut: no searchable annotations to be found on plain Java classes and core Spring types...if (AnnotationsScanner.hasPlainJavaAnnotationsOnly(annotation)) {return null;}// Exhaustive retrieval of merged annotations...return MergedAnnotations.from(annotation, new Annotation[] {annotation}, RepeatableContainers.none()).get(annotationType).withNonMergedAttributes().synthesize(AnnotationUtils::isSingleLevelPresent).orElse(null);} public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);return getAnnotation((AnnotatedElement) resolvedMethod, annotationType);}

find***方法源碼

public static <A extends Annotation> A findAnnotation(Class<?> clazz, @Nullable Class<A> annotationType) {if (annotationType == null) {return null;}// Shortcut: directly present on the element, with no merging needed?if (AnnotationFilter.PLAIN.matches(annotationType) ||AnnotationsScanner.hasPlainJavaAnnotationsOnly(clazz)) {return clazz.getDeclaredAnnotation(annotationType);}// Exhaustive retrieval of merged annotations...return MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none()).get(annotationType).withNonMergedAttributes().synthesize(MergedAnnotation::isPresent).orElse(null);}public static <A extends Annotation> A findAnnotation(Method method, @Nullable Class<A> annotationType) {if (annotationType == null) {return null;}// Shortcut: directly present on the element, with no merging needed?if (AnnotationFilter.PLAIN.matches(annotationType) ||AnnotationsScanner.hasPlainJavaAnnotationsOnly(method)) {return method.getDeclaredAnnotation(annotationType);}// Exhaustive retrieval of merged annotations...return MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY, RepeatableContainers.none()).get(annotationType).withNonMergedAttributes().synthesize(MergedAnnotation::isPresent).orElse(null);}public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, @Nullable Class<A> annotationType) {if (annotationType == null) {return null;}// Shortcut: directly present on the element, with no merging needed?if (AnnotationFilter.PLAIN.matches(annotationType) ||AnnotationsScanner.hasPlainJavaAnnotationsOnly(annotatedElement)) {return annotatedElement.getDeclaredAnnotation(annotationType);}// Exhaustive retrieval of merged annotations...return MergedAnnotations.from(annotatedElement, SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none()).get(annotationType).withNonMergedAttributes().synthesize(MergedAnnotation::isPresent).orElse(null);}

AnnotatedElementUtils 源碼分析

synthesizeAnnotation

synthesizeAnnotation,合成Annotation,通過獲取一個動態代理注解(相當于調用者傳進來的注解會被代理掉)來訪問Annotation,該方法是別名注解@AliasFor的核心原理。

final class TypeMappedAnnotation<A extends Annotation> extends AbstractMergedAnnotation<A> {protected A createSynthesized() {return SynthesizedMergedAnnotationInvocationHandler.createProxy(this, getType());}}static <A extends Annotation> A createProxy(MergedAnnotation<A> annotation, Class<A> type) {ClassLoader classLoader = type.getClassLoader();InvocationHandler handler = new SynthesizedMergedAnnotationInvocationHandler<>(annotation, type);Class<?>[] interfaces = isVisible(classLoader, SynthesizedAnnotation.class) ?new Class<?>[] {type, SynthesizedAnnotation.class} : new Class<?>[] {type};return (A) Proxy.newProxyInstance(classLoader, interfaces, handler);} final class SynthesizedMergedAnnotationInvocationHandler<A extends Annotation> implements InvocationHandler {//Annotationprivate final MergedAnnotation<?> annotation;annotation的類型private final Class<A> type;屬性方法private final AttributeMethods attributes;@Nullableprivate volatile Integer hashCode;private SynthesizedMergedAnnotationInvocationHandler(MergedAnnotation<A> annotation, Class<A> type) {Assert.notNull(annotation, "MergedAnnotation must not be null");Assert.notNull(type, "Type must not be null");Assert.isTrue(type.isAnnotation(), "Type must be an annotation");this.annotation = annotation;this.type = type;this.attributes = AttributeMethods.forAnnotationType(type);for (int i = 0; i < this.attributes.size(); i++) {getAttributeValue(this.attributes.get(i));}}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) {if (ReflectionUtils.isEqualsMethod(method)) {return annotationEquals(args[0]);}if (ReflectionUtils.isHashCodeMethod(method)) {return annotationHashCode();}if (ReflectionUtils.isToStringMethod(method)) {return this.annotation.toString();}if (isAnnotationTypeMethod(method)) {return this.type;}if (this.attributes.indexOf(method.getName()) != -1) {return getAttributeValue(method);}throw new AnnotationConfigurationException(String.format("Method [%s] is unsupported for synthesized annotation type [%s]", method, this.type));}private Object getAttributeValue(Method method) {String name = method.getName();Class<?> type = ClassUtils.resolvePrimitiveIfNecessary(method.getReturnType());return this.annotation.getValue(name, type).orElseThrow(() -> new NoSuchElementException("No value found for attribute named '" + name +"' in merged annotation " + this.annotation.getType().getName()));}

四中示例:

生成的TypeMappedAnnotation實例為:

@demon.study.A_C(c1=this is root, c2=this is root, c3=A_C_3, c4=A_C_4, c5=A_C_4, c6=A_C_6)

這是一個不存在的注解,因此生成一個代理類表示此注解。

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的Spring注解编程基石(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。