天河建设网站多少钱,泗阳做网站设计,泾县网站seo优化排名,给网站做选题文章目录何为反射#xff1f;反射的应用场景了解么#xff1f;谈谈反射机制的优缺点优点缺点反射实战获取 Class 对象的四种方式1. 知道具体类的情况下可以使用TargetObject.class#xff1a;2. 通过 Class.forName()传入类的全路径获取#xff1a;3. 通过对象实例instance…
文章目录何为反射反射的应用场景了解么谈谈反射机制的优缺点优点缺点反射实战获取 Class 对象的四种方式1. 知道具体类的情况下可以使用TargetObject.class2. 通过 Class.forName()传入类的全路径获取3. 通过对象实例instance.getClass()获取4. 通过类加载器xxxClassLoader.loadClass()传入类路径获取:反射的一些基本操作何为反射
如果说大家研究过框架的底层原理或者咱们自己写过框架的话一定对反射这个概念不陌生。
反射之所以被称为框架的灵魂主要是因为它赋予了我们在运行时分析类以及执行类中方法的能力。
通过反射你可以获取任意一个类的所有属性和方法你还可以调用这些方法和属性。
反射的应用场景了解么
像咱们平时大部分时候都是在写业务代码很少会接触到直接使用反射机制的场景。
但是这并不代表反射没有用。相反正是因为反射你才能这么轻松地使用各种框架。像 Spring/Spring Boot、MyBatis 等等框架中都大量使用了反射机制。
这些框架中也大量使用了动态代理而动态代理的实现也依赖反射。
比如下面是通过 JDK 实现动态代理的示例代码其中就使用了反射类 Method 来调用指定的方法。
另外像 Java 中的一大利器 注解 的实现也用到了反射。
为什么你使用 Spring 的时候 一个Component注解就声明了一个类为 Spring Bean 呢为什么你通过一个 Value注解就读取到配置文件中的值呢究竟是怎么起作用的呢
这些都是因为你可以基于反射分析类然后获取到类/属性/方法/方法的参数上的注解。你获取到注解之后就可以做进一步的处理。
谈谈反射机制的优缺点
优点
可以让咱们的代码更加灵活、为各种框架提供开箱即用的功能提供了便利
缺点
让我们在运行时有了分析操作类的能力这同样也增加了安全问题。比如可以无视泛型参数的安全检查泛型参数的安全检查发生在编译时。另外反射的性能也要稍差点不过对于框架来说实际是影响不大的。
反射实战
获取 Class 对象的四种方式
如果我们动态获取到这些信息我们需要依靠 Class 对象。Class 类对象将一个类的方法、变量等信息告诉运行的程序。Java 提供了四种方式获取 Class 对象:
1. 知道具体类的情况下可以使用TargetObject.class
Class alunbarClass TargetObject.class;但是我们一般是不知道具体类的基本都是通过遍历包下面的类来获取 Class 对象通过此方式获取 Class 对象不会进行初始化
2. 通过 Class.forName()传入类的全路径获取
Class alunbarClass1 Class.forName(“com.zxn.TargetObject”); Copy to clipboardErrorCopied
3. 通过对象实例instance.getClass()获取
TargetObject o new TargetObject();
Class alunbarClass2 o.getClass();4. 通过类加载器xxxClassLoader.loadClass()传入类路径获取:
ClassLoader.getSystemClassLoader().loadClass(com.zxn.TargetObject);通过类加载器获取 Class 对象不会进行初始化意味着不进行包括初始化等一系列步骤静态代码块和静态对象不会得到执行
反射的一些基本操作
1、创建一个我们要使用反射操作的类 TargetObject。 package com.zxn;public class TargetObject {private String value;public TargetObject() {value JavaGuide;}public void publicMethod(String s) {System.out.println(I love s);}private void privateMethod() {System.out.println(value is value);}
}
2、使用反射操作这个类的方法以及参数
package com.zxn;import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class Main {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException {/*** 获取 TargetObject 类的 Class 对象并且创建 TargetObject 类实例*/Class? targetClass Class.forName(com.zxn.TargetObject);TargetObject targetObject (TargetObject) targetClass.newInstance();/*** 获取 TargetObject 类中定义的所有方法*/Method[] methods targetClass.getDeclaredMethods();for (Method method : methods) {System.out.println(method.getName());}/*** 获取指定方法并调用*/Method publicMethod targetClass.getDeclaredMethod(publicMethod,String.class);publicMethod.invoke(targetObject, ZXN);/*** 获取指定参数并对参数进行修改*/Field field targetClass.getDeclaredField(value);//为了对类中的参数进行修改我们取消安全检查field.setAccessible(true);field.set(targetObject, JavaGuide);/*** 调用 private 方法*/Method privateMethod targetClass.getDeclaredMethod(privateMethod);//为了调用private方法我们取消安全检查privateMethod.setAccessible(true);privateMethod.invoke(targetObject);}
} 文章转载自: http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.rqckh.cn.gov.cn.rqckh.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.ptzf.cn.gov.cn.ptzf.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.dkslm.cn.gov.cn.dkslm.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.srjbs.cn.gov.cn.srjbs.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.wqngt.cn.gov.cn.wqngt.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.xyhql.cn.gov.cn.xyhql.cn