含义
反射(reflection)机制是指计算机程序在运行时(Run time)可以访问、检测和修改它本身状态或行为的一种能力。用比喻来说,那种程序能够“观察”并且修改自己的行为.
在面向对象的编程语言如Java中,反射允许在编译期间不知道接口(类)的名称,字段、方法的情况下在运行时检查类、接口、字段和方法。这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制.
获取Class对象的三种方法
public class Student {
private String id;
private String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public void study(String worlds) {
System.out.println("students should study" + worlds);
}
}
1) Class.forName(),Class类的forName()方法
Class<?> studentClass = Class.forName("com.dgut.reflection.Student");
2)object.getClass(),对象的getClass()方法
Student student = new Student(1001,"lucy");
Class<? extends Student> studentClass = student.getClass();
3)Object.class,类的静态属性
Class<Student> studentClass = Student.class;
获取类内信息
1.获取类的字段信息
/*1. 获取所有的公有属性*/
Field[] fields1 = studentClass.getFields();
/*2. 获取所有的属性,包含私有属性*/
Field[] fields = studentClass.getDeclaredFields();
/*3. 根据名称获取类的某个属性*/
Field id = studentClass.getDeclaredField("id");
2.获取类的构造函数
/*1. 获取所有的构造函数*/
Constructor<?>[] constructors = studentClass.getDeclaredConstructors();
/*2. 根据参数获取对应的构造函数*/
Constructor<?> constructor = studentClass.getConstructor(Integer.class, String.class);
3.获取类的方法
/*1. 获取所有的公有方法*/
Method[] methods = studentClass.getMethods();
/*2. 获取所有的方法,包含私有方法*/
Method[] declaredMethods = studentClass.getDeclaredMethods();
/*3. 根据名称和参数获取类的某个方法*/
Method study = studentClass.getDeclaredMethod("study", String.class);
4.获取注解信息
Annotation[] annotations = studentClass.getAnnotations();
使用反射机制生成对象
Student s = (Student)constructor.newInstance(1001,"jack");
使用反射机制调用方法
Method study = studentClass.getDeclaredMethod("study", String.class);
Student s = (Student)constructor.newInstance(1001,"jack");
/**
* 参数一: 调用方法的对象
* 参数二: 方法的参数
*/
study.invoke(s, "English");
使用反射机制访问成员变量
//获取某个具体属性
Field id = studentClass.getDeclaredField("id");
//使用构造器生成对象
Student s = (Student)constructor.newInstance(1001,"jack");
//取消安全检查
id.setAccessible(true);
//对id重新设值
id.set(s,1002);
System.out.println(s.getId());
output:
1002
反射的应用场景
在JDBC 的操作中,如果要想进行数据库的连接,则必须按照以上的几步完成
- 通过Class.forName()加载数据库的驱动程序 (通过反射加载,前提是引入相关了Jar包)
- 通过 DriverManager 类进行数据库的连接,连接的时候要输入数据库的连接地址、用户名、密码
- 通过Connection 接口接收连接
public class ConnectionJDBC {
//驱动程序就是之前在classpath中配置的JDBC的驱动程序的JAR 包中
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
//连接地址是由各个数据库生产商单独提供的,所以需要单独记住
public static final String DBURL = "jdbc:mysql://localhost:3306/test";
//连接数据库的用户名
public static final String DBUSER = "root";
//连接数据库的密码
public static final String DBPASS = "";
public static void main(String[] args) throws Exception {
Connection con = null; //表示数据库的连接对象
Class.forName(DBDRIVER); //1、使用CLASS 类加载驱动程序 ,反射机制的体现
con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); //2、连接数据库
System.out.println(con);
con.close(); // 3、关闭数据库
}
在 Java的反射机制在做基础框架的时候非常有用,行内有一句这样的老话:反射机制是Java框架的基石。最经典的就是xml的配置模式。
Spring 通过 XML 配置模式装载 Bean 的过程:
- 将程序内所有 XML 或 Properties 配置文件加载入内存中
- Java类里面解析xml或properties里面的内容,得到对应实体类的字节码字符串以及相关的属性信息
- 使用反射机制,根据这个字符串获得某个类的Class实例
- 动态配置实例的属性
在Spring的配置文件中,经常看到如下配置:
<bean id="studentDao" class="com.dgut.reflect.Dao.impl.studentDaoImpl"></bean>
下面是Spring通过配置进行实例化对象,并放到容器中的伪代码:
//解析<bean .../>元素的id属性得到该字符串值为“studentDao”
String idStr = "studentDao";
//解析<bean .../>元素的class属性得到该字符串值为“com.dgut.reflect.Dao.impl.studentDaoImpl”
String classStr = "com.dgut.reflect.Dao.impl.studentDaoImpl";
//利用反射知识,通过classStr获取Class类对象
Class<?> cls = Class.forName(classStr);
//实例化对象
Object obj = cls.newInstance();
//container表示Spring容器
container.put(idStr, obj);
当一个类里面需要应用另一类的对象时,Spring的配置如下所示:
<bean id="studentService" class="com.dgut.reflect.Dao.impl.studentServiceImpl">
<!-- 控制调用setStudentDao()方法,将容器中的studentDao bean作为传入参数 -->
<property name="studentDao" ref="studentDao"></property>
</bean>
我们继续用伪代码的形式来模拟实现一下Spring底层处理原理:
//解析<property .../>元素的name属性得到该字符串值为“studentDao”
String nameStr = "studentDao";
//解析<property .../>元素的ref属性得到该字符串值为“studentDao”
String refStr = "studentDao";
//生成将要调用setter方法名
String setterName = "set" + nameStr.substring(0, 1).toUpperCase()
+ nameStr.substring(1);
//获取spring容器中名为refStr的Bean,该Bean将会作为传入参数
Object paramBean = container.get(refStr);
//获取setter方法的Method类,此处的cls是刚才反射代码得到的Class对象
Method setter = cls.getMethod(setterName, paramBean.getClass());
//调用invoke()方法,此处的obj是刚才反射代码得到的Object对象
setter.invoke(obj, paramBean);
Spring这样做的好处是:
- 不用每一次都要在代码里面去new或者做其他的事情
- 以后要改的话直接改配置文件,代码维护起来就很方便了
- 有时为了适应某些需求,Java类里面不一定能直接调用另外的方法,可以通过反射机制来实现
反射的优缺点
优点:在运行时获得类的各种内容,进行反编译,对于Java这种先编译再运行的语言,能够让我们很方便的创建灵活的代码,这些代码可以在运行时装配,无需在组件之间进行源代码的链接,更加容易实现面向对象。
缺点:(1)反射会消耗一定的系统资源,因此,如果不需要动态地创建一个对象,那么就不需要用反射;(2)反射调用方法时可以忽略权限检查,因此可能会破坏封装性而导致安全问题。(3)内部暴露:由于反射允许代码执行一些在正常情况下不被允许的操作(比如访问私有的属性和方法),所以使用反射可能会导致意料之外的副作用–代码有功能上的错误,降低可移植性。反射代码破坏了抽象性,因此当平台发生改变的时候,代码的行为就有可能也随着变化。