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

What is the analysis of Springboot source code?

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

Share

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

Springboot source code is what analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Summary:

We all know that annotations implement the java.lang.annotation.Annotation interface, seeing is believing, hearing is false, and sometimes seeing is not necessarily true.

/ * The common interface extended by all annotation types. Note that an * interface that manually extends this one does not define * an annotation type. Also note that this interface does not itself * define an annotation type. * * More information about annotation types can be found in section 9.6 of * The Java ™Language Specification. * * The {@ link java.lang.reflect.AnnotatedElement} interface discusses * compatibility concerns when evolving an annotation type from being * non-repeatable to being repeatable. * * @ author Josh Bloch * @ since 1.5 * / meta note:

Meta-annotations are generally used to specify information such as the life cycle and target of an annotation. As with source code comments, if custom annotations are not much different from normal comments without adding meta annotations, meta annotations will allow the compiler to compile information into bytecode files.

@ Target

@ Target is used to indicate where the modified annotation can eventually work.

ElementType is an enumerated type

ElementType.TYPE: class Interface (including annotation type) or enumeration declaration ElementType.FIELD: field declaration (including enumeration constant) ElementType.METHOD: method declaration ElementType.PARAMETER: formal parameter declaration ElementType.CONSTRUCTOR: constructor declaration ElementType.LOCAL_VARIABLE: local local variable declaration ElementType.ANNOTATION_TYPE: annotation declaration ElementType.PACKAGE: package declaration ElementType.TYPE_PARAMETER: type parameter declaration jdk1. 8 add ElementType.TYPE_USE: add using a type of jdk1.8

@ Retention

@ Retention is used to indicate the life cycle of the current annotation

RetentionPolicy is an enumerated type

RetentionPolicy.SOURCE: the compiler discards comments. RetentionPolicy.CLASS: comments will be recorded by the compiler in the class file, but do not need to be retained by VM at run time. RetentionPolicy.RUNTIME: comments are recorded by the compiler in the class file and retained by VM at run time, so they can be read reflectively.

@ Documented

@ Documented indicates that comments with types will be recorded by javadoc and similar tools by default. This type should be used to annotate the elements of the declarative client that affect the type used by the annotation. If you declare a record with an annotation type, its annotation becomes part of the comment element of the public API.

@ Inherited

@ Inherited means that annotation types are automatically inherited. If there is an inherited meta-annotation declaration on the annotation type, the user queries the annotation type declaration of the class, and the class declaration does not have an annotation of this type, and then automatically queries the superclass annotation type of the class. This process is repeated until the type is found for this annotation, or the top (object) of the class hierarchy is reached. If no superclass has this type of annotation, the query indicates that the problematic class does not have such an annotation. Note that if annotated, this meta-annotation type is not valid type is used to annotate anything except the class. Also note that this meta-annotation will only cause the annotation to be inherited from the superclass; annotations on the implemented interface have no effect.

Annotation implementation

How do I customize annotations?

Package com.github.dqqzj.springboot.annotation; import org.springframework.core.annotation.AliasFor; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target / * @ author qinzhongjian * @ date created in 2019-07-28 07:54 * @ description: TODO * @ since JDK 1.8.0_212-b10 * / @ Target (value = {ElementType.TYPE}) @ Retention (value = RetentionPolicy.RUNTIME) @ Component public @ interface Hello {@ AliasFor Annotation = Component.class) String value () default "hi" }

How do I get annotation element information?

The annotations shown in the figure above actually use proxies and are JDK proxies.

Analysis of annotation principle

Since it is a run-time generated proxy class, we can add System.setProperty ("sun.misc.ProxyGenerator.saveGeneratedFiles", "true") or

Let's analyze the generated proxy class

Package com.sun.proxy; import com.github.dqqzj.springboot.annotation.Hello; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy1 extends Proxy implements Hello {private static Method M1; private static Method m2; private static Method M4; private static Method m0; private static Method m3 Public $Proxy1 (InvocationHandler var1) throws {super (var1);} public final boolean equals (Object var1) throws {try {return (Boolean) super.h.invoke (this, M1, new Object [] {var1});} catch (RuntimeException | Error var3) {throw var3 } catch (Throwable var4) {throw new UndeclaredThrowableException (var4);}} public final String toString () throws {try {return (String) super.h.invoke (this, m2, (Object []) null);} catch (RuntimeException | Error var2) {throw var2 } catch (Throwable var3) {throw new UndeclaredThrowableException (var3);}} public final Class annotationType () throws {try {return (Class) super.h.invoke (this, M4, (Object []) null);} catch (RuntimeException | Error var2) {throw var2 } catch (Throwable var3) {throw new UndeclaredThrowableException (var3);}} public final int hashCode () throws {try {return (Integer) super.h.invoke (this, M0, (Object []) null);} catch (RuntimeException | Error var2) {throw var2 } catch (Throwable var3) {throw new UndeclaredThrowableException (var3);}} public final String value () throws {try {return (String) super.h.invoke (this, m3, (Object []) null);} catch (RuntimeException | Error var2) {throw var2 } catch (Throwable var3) {throw new UndeclaredThrowableException (var3);}} static {try {M1 = Class.forName ("java.lang.Object"). GetMethod ("equals", Class.forName ("java.lang.Object")); m2 = Class.forName ("java.lang.Object"). GetMethod ("toString") M4 = Class.forName ("com.github.dqqzj.springboot.annotation.Hello") .getMethod ("annotationType"); M0 = Class.forName ("java.lang.Object") .getMethod ("hashCode"); m3 = Class.forName ("com.github.dqqzj.springboot.annotation.Hello") .getMethod ("value") } catch (NoSuchMethodException var2) {throw new NoSuchMethodError (var2.getMessage ());} catch (ClassNotFoundException var3) {throw new NoClassDefFoundError (var3.getMessage ());}

The InvocationHandler here is actually our AnnotationInvocationHandler, and there is a memberValues, which is a Map key-value pair, and the key is the name of the attribute we annotated, and the value is the value on which the property was originally assigned. Next, I'll debug the code to share with you the secret.

Hello hello = TestAnnotation.class.getAnnotation (Hello.class) this part of the debugging code I will ignore direct debugging

The related methods of AnnotationInvocationHandler.

Preparation for annotating mysteries

Decompiling the annotation file, it is found that the annotation does implement the Annotation interface

Those who are familiar with the jdk specification will find that the bottom s#7RuntimeVisibleAnnotations is the annotation information that can be accessed at run time and can be obtained by reflection.

The virtual machine specification defines a series of property sheets related to annotations, whether fields, methods, or classes themselves, which can be written into bytecode files if annotated. There are several types of property sheets:

RuntimeVisibleAnnotations: runtime visible annotations RuntimeInVisibleAnnotations: runtime invisible annotations RuntimeVisibleParameterAnnotations: runtime visible method parameters annotations RuntimeInVisibleParameterAnnotations: runtime invisible method parameters annotations AnnotationDefault: default values of annotated class elements `Annotation Mystery debugging

Description: obviously there is only one @ Hello note why there are two proxy classes on the left. In this place, there will be one more proxy class.

Public final class $Proxy0 extends Proxy implements Retention {/ / omit extraneous code. }

How reflection annotations work:

We can assign values to annotation properties in the form of key-value pairs, like this: @ Hello (value = "hi")

Modify an element with annotations, and the compiler will scan the annotations on each class or method at compile time, and will do a basic check to see if your annotations are allowed to act in the current position, and finally write the annotation information into the element's property table.

The virtual machine takes out the comments in the RUNTIME in the life cycle and generates a proxy class that implements the annotation interface through the dynamic proxy mechanism.

How to dynamically modify the proxy value?

Now that we know that the value of the annotation is stored in Map memberValues, we can use reflection to get and reassign the value.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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