The definition of the principle of Java reflection and the auto-injected reflection of spring
The reflection mechanism of Java is in the running state,
- All properties and methods of this class are known to any class;
- For any object, you can call any of its methods and properties.
This dynamic acquisition of information and the ability to dynamically invoke the object's methods is called the reflection mechanism of the Java language.
The principle of automatic injection of sping
A Bean's class:
public class User{ private String username; private String mobile; public String getUsername(){ return username; } public void setUsername(String username){ this.username = username; } public String getMobile(){ return mobile; } public void setMobile(String mobile){ this.mobile = mobile; }}
Driver class:
import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class ReflectionTest { @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) throws Exception { //id 为自己指定的值 String id = "user"; Map<String, Object> context = new HashMap<String, Object>(); //这里是通过User.class.getName()获取类名,通用的作法是通过配置文件获取类名 //三种方法,可以获得类名 //1. User.class.getName() //2. new User().getName() //3. Class.forName("User").getName() Class c = Class.forName(User.class.getName()); Object object = c.newInstance(); // 模拟spring容器 context.put(id, object); Method method = c.getDeclaredMethod("setUsername", String.class); // setter注入 method.invoke(object, "xiaoqiang"); System.out.println(((User) object).getUsername()); }}
Understanding code for reflection Definitions
In the running state,
All methods and properties of this class are known to any class
The
Code example is as follows:
Import Java.lang.reflect.Field; Import Java.lang.reflect.method;public class Reflectiontest {@SuppressWarnings ("rawtypes") public static void main ( String[] args) throws Exception {Class c = class.forname (User.class.getName ()); System.out.println ("------------------------------------"); Get all the methods defined below for all user classes method method[] = C.getdeclaredmethods (); for (int i=0;i<method.length;i++) {System.out.println (Method[i].getname ()); } System.out.println ("------------------------------------"); Gets all properties defined under all user Classes Field f[]=c.getdeclaredfields (); for (int i=0;i<f.length;i++) {System.out.println (F[i].getname ()); } System.out.println ("------------------------------------"); Get all methods defined under all user classes and methods of the parent class method method1[] = C.getmethods (); for (int i=0;i<method1.length;i++) {System.out.println (Method1[i].getname ()); } System.out.println("------------------------------------"); Gets all properties defined under all user classes and the properties of the parent class Field f1[]=c.getdeclaredfields (); for (int i=0;i<f1.length;i++) {System.out.println (F1[i].getname ()); } }}
For any object, you can call any of its properties and methods
The code example is as follows:
import java.lang.reflect.Field;public class ReflectTest{ public static void main(String[] args) throws Exception { Class c = Class.forName(User.class.getName()); Object obj = c.newInstance(); Field field = c.getDeclaredField("username"); field.setAccessible(true); field.set(object,"hehe"); System.out.println((User)object.getUsername()); }}
At this point, username is set to hehe.
Resources:
Java reflection, Spring Auto-injection principle
The principle of reflection in Java and Spring Automatic injection (reprint)