1.什么是反射
反射是框架设计的灵魂
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
反射就是在类运行期间,把类中成员抽取为其他类的过程。
常见的框架有:mybatis: 持久化框架;springmvc:mvc框架;以及spring框架
使用类加载器ClassLoader将字节码文件加载到内存中加载完毕后,在jvm中以Class的形式存在
Student.java经过了jdk的那些过程?
Student.java(源码)------javac-----Student.class(字节码文件)-----java命令—可执行文件
2.为什么使用反射
反射是为了解决在运行期,对某个实例一无所知的情况下,如何调用其方法或属性。
例子: spring框架中只需要传入类的路径----spring框架就会帮你创建类的对象。
3. 获取Class反射类以及Class类中的常用方法
3.1 获取Class反射类
获取Class反射类有三种方式:
第一种: 通过类名.class属性
Class<Student> aClass = Student.class;
第二种: 通过类路径获取
Class aClass = Class.forName("com.lcy.test.Student");
第三种: 通过对象名获取反射类型
Student student = new Student();
Class aClass = student.getClass();
3.2 Class类中的常用方法
1.根据反射类得到实例对象 newInstance()
// 通过反射类创建类对象
Student student = aClass.newInstance();
2.得到反射类上的注解对象 getAnnotation()
//获取反射类上的注解对象---反射是在运行时得到
MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class);
4. 获取Method方法类对象以及其中常用的方法
4.1 获取Method方法类对象
获取Method方法类对象有四种方法,根据不同的需要使用不同的方法。
Class<Student> aClass = Student.class;
1. 得到本类中定义的所有Method类对象
getDeclaredMethods(): 得到本类中所有的方法
//得到本类中定义的所有Method类对象
Method[] declaredMethods = aClass.getDeclaredMethods();
2. 获取本类中指定的方法对象
getDeclaredMethod(“方法名”,参数类型):获取本类中指定的方法对象
//获取本类中指定的方法对象
Method fun = aClass.getDeclaredMethod("fun",Integer.class);
3. 获取本类以及父辈类中所以public方法对象
getMethods():获取本类以及父辈类中public修饰的方法
//获取本类以及父类中所以public方法对象
Method[] methods = aClass.getMethods();
4. 获取本类以及父类中指定的public方法对象
getMethod(“方法名”,参数类型):获取本类以及父辈类中指定public修饰的方法
//获取本类以及父类中指定的public方法对象
Method method = aClass.getMethod("equals", Object.class);
4.2 Method类对象中常用的方法
1. 调用类中的方法
Object r=invoke(Object对象,参数值) 调用类中的方法
Student student = class.newInstance();
Method getAge = class.getDeclaredMethod("getAge", Integer.class);
// 调用获取的方法,传入相应的参数,返回方法返回的数据
Object invoke = getAge.invoke(student, 20);
2. 设置允许访问私有成员
setAccessible()设置允许访问私有成员
Method getAge = class.getDeclaredMethod("getAge", Integer.class);
// 设置允许访问私有成员
getAge.setAccessible(true);
Object invoke = getAge.invoke(student, 18);
5. 获取Field属性对象的方式及Field类常用的方法
5.1 获取Field属性对象的方式
获取Field属性对象的方式有四种,与获取Method方法类对象的方式相似。
1. 获取本类中所有的属性
getDeclaredFields(): 获取本类中所有的属性
// 获取本类中所有的属性
Field[] declaredFields = class.getDeclaredFields();
2. 获取本类中指定的属性
getDeclaredField(“属性名”):获取本类中指定的属性
// 获取本类中指定的属性
Field name = class.getDeclaredField("name");
3. 获取本类以及父辈类中public修饰的属性
getFields():获取本类以及父辈类中public修饰的属性。
// 获取本类以及父辈类中public修饰的属性
Field[] fields = class.getFields();
4. 获取本类以及父辈类中指定public修饰的属性。
getField(“属性名”):获取本类以及父辈类中指定public修饰的属性。
// 获取本类以及父辈类中指定public修饰的属性
Field name = class.getField("name");
5.2 Field类中常用的方法
1. 为属性赋值
set(Object对象,值):为属性赋值
// 通过类名.class属性获取反射类
Class<Student> class = Student.class;
Student student = class.newInstance();
// 获取本类中指定的属性
Field name = class.getDeclaredField("name");
//设置允许访问私有成员
name.setAccessible(true);
// 为属性赋值
name.set(student,"张三");
2. 获取属性名
getName():获取属性名
//获取属性名
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
//获取属性名
String name = declaredField.getName()
}
3. 获取属性上的注解对象
getAnnotation():获取属性上的注解对象
//获取每个属性对象上的注解对象
MyAnnotation annotation = declaredField.getAnnotation(MyAnnotation.class);
//获取注解对象的value
String value = annotation.value();
6. 案例
在properties属性文件中指定类的路径,通过反射完成类对象的创建以及类中属性的赋值
public class Test {
public static void main(String[] args) throws Exception {
//1.加载属性文件
InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream("test.properties");
//2.通过属性类Properties
Properties properties=new Properties();
properties.load(resourceAsStream);
//3.属性类读取属性文件中指定的key值
String className = properties.getProperty("className");
//4.根据类路径得到反射对象
Class<?> aClass = Class.forName(className);
//5.根据反射类创建类对象
Object o = aClass.newInstance();
System.out.println(o);
//6.为属性赋值
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
field.set(o,new Random(1000)+"");
System.out.println(o);
}
}
}