Annotation and reflection notes

Annotation and reflection are the bottom layer of all frameworks.
Frame: mybatis, spring, spring boot...
annotation

Getting started with annotations

Introduced in jdk5.0, currently jdk12
can be read by other programs (such as compilers) -it can achieve dynamics.
Read annotations through reflection mechanism

Built-in annotations

@Override
@Deprecated
@SuppressWarning
suppress 镇压

@SuppressWarnings("serial
") The role of @SuppressWarnings("serial")

Custom annotations, meta annotations

Meta-annotation: There are 4 @Targets for annotation
meta-annnotation to describe where the annotation can be used, class, method, constructor...


@Retention
retention is
used to describe when the annotation is still valid, usually Runtime
Source<Class<Runtime

@Document
indicates whether the annotation can be generated in javadoc

@Inherited
inherited
indicates that the subclass can inherit the annotation in the parent class

Custom annotation:
@interface
annotated parameter: parameter type + parameter name ();

//测试元注解
public class Test {
    
    

	@MyAnnotation(name = "duan", school = {
    
    "aa","bb"})
	//定义了参数必须写参数,此处显示赋值,但默认值已写,就可不写
	public void test() {
    
    }

	@MyAnnotation("cc")
	//若注解只有一个参数,可定义为value,然后此处的value可省略(必须为value才可省略啊)
	public void test() {
    
    }
	
}

//定义一个注解
@Target({
    
    ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    
    
	//注解的参数,不是方法
	String name() default "";//默认为空
	int age() default "";
	int id() default -1;//默认值是-1,代表不存在
	String[] schools();
}

@Target({
    
    ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    
    
	String value();
}

reflection

Make the java language dynamic

Overview

Static language: Immutable structure at runtime. There are
dynamic languages such as Java, C, and C++ : the code can be changed by itself during runtime. There are: Object-C, C##, JavaScript, PHP, Python

Java can be called a "quasi-dynamic language", that is, it has a certain degree of dynamics.

Reflection: The
reflection mechanism allows the program to use the Reflection API to obtain the internal information of any class during the execution period, and can directly manipulate the internal properties and methods of any object. (Including private)

After the class is loaded, an object of type Class is generated in the method area of ​​the heap memory (a class has only one Class object), and this object contains the complete structure information of the class. We can see the structure of the class through this object. This object is like a mirror, through which the structure of the class can be seen. That is, reflection.

//
public class Test {
    
    
	//获取Class对象
	Class c1 = Class.forName("com.kuang.reflection.User");//包名+类名,会抛异常
}

//实体类:POJO、entity
class User {
    
    
	private String name;
	private int id;
	private int age;

	public User() {
    
    }

	public User(String name, int id, int age){
    
    ...}
	...//get、set
	@Override
	public String toString(){
    
    ...}
	
}

After a class is loaded, the entire structure of the class will be encapsulated in the Class object

Understand the Class class, get the Class instance (emphasis)

Common methods of the
Insert picture description here
Class class : get an instance of the Class class:
1.

Class clazz = Person.class;
Class clazz = person.getClass();

3. Known full class name: package + name, ClassNotFoundException may be thrown

Class clazz = Class.forName("demo01.Student");

4. The Type attribute of the packaging class of the basic data type

Class c4 = Integer.TYPE;

5.ClassLoader

Class loading, ClassLoader


Insert picture description here
The loading process of java memory class:
Insert picture description here
Insert picture description here

Create objects of runtime class (emphasis)

Get the complete structure of the runtime class

Call the specified structure of the runtime class (emphasis)

Application of reflection: dynamic proxy

Guess you like

Origin blog.csdn.net/weixin_40816843/article/details/113252399