Java反射机制及IoC原理

来源:互联网 发布:unity 2d优化视频 编辑:程序博客网 时间:2024/05/16 15:06

  • 一. 反射机制概念

  主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。在java中,只要给定类的名字, 那么就可以通过反射机制来获得类的所有信息。

  反射是Java中一种强大的工具,能够使我们很方便的创建灵活的代码,这些代码可以再运行时装配,无需在组件之间进行源代码链接。但是反射使用不当会成本很高!

  类中有什么信息,利用反射机制就能可以获得什么信息,不过前提是得知道类的名字。

二. 反射机制的作用

  1. 在运行时判断任意一个对象所属的类;
  2. 在运行时获取类的对象;
  3. 在运行时访问java对象的属性,方法,构造方法等。

三. 反射机制的优点与缺点

首先要搞清楚为什么要用反射机制?直接创建对象不就可以了吗,这就涉及到了动态与静态的概念。 
静态编译:在编译时确定类型,绑定对象,即通过。 
动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多态的应用,有以降低类之间的藕合性。 

反射机制的优点:可以实现动态创建对象和编译,体现出很大的灵活性(特别是在J2EE的开发中它的灵活性就表现的十分明显)。通过反射机制我们可以获得类的各种内容,进行了反编译。对于JAVA这种先编译再运行的语言来说,反射机制可以使代码更加灵活,更加容易实现面向对象。

  比如,一个大型的软件,不可能一次就把把它设计的很完美,当这个程序编译后,发布了,当发现需要更新某些功能时,我们不可能要用户把以前的卸载,再重新安装新的版本,假如这样的话,这个软件肯定是没有多少人用的。采用静态的话,需要把整个程序重新编译一次才可以实现功能的更新,而采用反射机制的话,它就可以不用卸载,只需要在运行时才动态的创建和编译,就可以实现该功能。 

反射机制的缺点:对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它 满足我们的要求。这类操作总是慢于只直接执行相同的操作。

四. 反射机制的示例

1.通过一个对象获得完整的包名和类名

添加一句:所有类的对象其实都是Class的实例。

复制代码
 1 package Reflect; 2  3 class Demo{ 4     //other codes... 5 } 6  7 class hello{ 8     public static void main(String[] args) { 9         Demo demo=new Demo();10         System.out.println(demo.getClass().getName());11     }12 }13 //【运行结果】:Reflect.Demo
复制代码

2.实例化Class类对象

复制代码
 1 package Reflect; 2  3 class Demo{ 4     //other codes... 5 } 6  7 class hello{ 8     public static void main(String[] args) { 9         Class<?> demo1=null;10         Class<?> demo2=null;11         Class<?> demo3=null;12         try{13             //一般尽量采用这种形式14             demo1=Class.forName("Reflect.Demo");15         }catch(Exception e){16             e.printStackTrace();17         }18         demo2=new Demo().getClass();19         demo3=Demo.class;20 21         System.out.println("类名称   "+demo1.getName());22         System.out.println("类名称   "+demo2.getName());23         System.out.println("类名称   "+demo3.getName());24     }25 }26 //【运行结果】:27 //类名称   Reflect.Demo28 //类名称   Reflect.Demo29 //类名称   Reflect.Demo
复制代码

3.通过Class实例化其他类的对象

复制代码
 1 package Reflect; 2  3 class Person{ 4     public String getName() { 5         return name; 6     } 7     public void setName(String name) { 8         this.name = name; 9     }10     public int getAge() {11         return age;12     }13     public void setAge(int age) {14         this.age = age;15     }16     @Override17     public String toString(){18         return "["+this.name+"  "+this.age+"]";19     }20     private String name;21     private int age;22 }23 24 class hello{25     public static void main(String[] args) {26         Class<?> demo=null;27         try{28             demo=Class.forName("Reflect.Person");29         }catch (Exception e) {30             e.printStackTrace();31         }32         Person per=null;33         try {34             per=(Person)demo.newInstance();35         } catch (InstantiationException e) {36             // TODO Auto-generated catch block37             e.printStackTrace();38         } catch (IllegalAccessException e) {39             // TODO Auto-generated catch block40             e.printStackTrace();41         }42         per.setName("Rollen");43         per.setAge(20);44         System.out.println(per);45     }46 }47 //【运行结果】:48 //[Rollen  20]
复制代码

但是注意一下,当我们把Person中的默认的无参构造函数取消的时候,比如自己定义只定义一个有参数的构造函数之后,会出现错误:

比如定义了一个构造函数:

1 public Person(String name, int age) {2         this.age=age;3         this.name=name;4     }

然后继续运行上面的程序,会出现:

复制代码
java.lang.InstantiationException: Reflect.Personat java.lang.Class.newInstance0(Class.java:340)at java.lang.Class.newInstance(Class.java:308)at Reflect.hello.main(hello.java:39)Exception in thread "main" java.lang.NullPointerExceptionat Reflect.hello.main(hello.java:47)
复制代码

所以大家以后再编写使用Class实例化其他类的对象的时候,一定要自己定义无参的构造函数。

4.通过Class调用其他类中的构造函数 (也可以通过这种方式通过Class创建其他类的对象)

复制代码
 1 package Reflect; 2  3 import java.lang.reflect.Constructor; 4  5 class Person{ 6     public Person() {    7  8     } 9     public Person(String name){10         this.name=name;11     }12     public Person(int age){13         this.age=age;14     }15     public Person(String name, int age) {16         this.age=age;17         this.name=name;18     }19     public String getName() {20         return name;21     }22     public int getAge() {23         return age;24     }25     @Override26     public String toString(){27         return "["+this.name+"  "+this.age+"]";28     }29     private String name;30     private int age;31 }32 33 class hello{34     public static void main(String[] args) {35         Class<?> demo=null;36         try{37             demo=Class.forName("Reflect.Person");38         }catch (Exception e) {39             e.printStackTrace();40         }41         Person per1=null;42         Person per2=null;43         Person per3=null;44         Person per4=null;45         //取得全部的构造函数46         Constructor<?> cons[]=demo.getConstructors();47         try{48             per1=(Person)cons[0].newInstance();49             per2=(Person)cons[1].newInstance("Rollen");50             per3=(Person)cons[2].newInstance(20);51             per4=(Person)cons[3].newInstance("Rollen",20);52         }catch(Exception e){53             e.printStackTrace();54         }55         System.out.println(per1);56         System.out.println(per2);57         System.out.println(per3);58         System.out.println(per4);59     }60 }61 //【运行结果】:62 //[null  0]63 //[Rollen  0]64 //[null  20]65 //[Rollen  20]
复制代码

5.返回一个类实现的接口

复制代码
 1 package Reflect; 2  3 interface China{ 4     public static final String name="Rollen"; 5     public static  int age=20; 6     public void sayChina(); 7     public void sayHello(String name, int age); 8 } 9 10 class Person implements China{11     public Person() {12 13     }14     public Person(String sex){15         this.sex=sex;16     }17     public String getSex() {18         return sex;19     }20     public void setSex(String sex) {21         this.sex = sex;22     }23     @Override24     public void sayChina(){25         System.out.println("hello ,china");26     }27     @Override28     public void sayHello(String name, int age){29         System.out.println(name+"  "+age);30     }31     private String sex;32 }33 34 class hello{35     public static void main(String[] args) {36         Class<?> demo=null;37         try{38             demo=Class.forName("Reflect.Person");39         }catch (Exception e) {40             e.printStackTrace();41         }42         //保存所有的接口43         Class<?> intes[]=demo.getInterfaces();44         for (int i = 0; i < intes.length; i++) {45             System.out.println("实现的接口   "+intes[i].getName());46         }47     }48 }49 //【运行结果】:50 //实现的接口   Reflect.China
复制代码

(以下几个例子,都会用到这个例子的Person类,所以为节省篇幅,此处不再粘贴Person的代码部分,只粘贴主类hello的代码)

6.取得其他类中的父类

复制代码
 1 class hello{ 2     public static void main(String[] args) { 3         Class<?> demo=null; 4         try{ 5             demo=Class.forName("Reflect.Person"); 6         }catch (Exception e) { 7             e.printStackTrace(); 8         } 9         //取得父类10         Class<?> temp=demo.getSuperclass();11         System.out.println("继承的父类为:   "+temp.getName());12     }13 }14 //【运行结果】15 //继承的父类为:   java.lang.Object
复制代码

7.获得其他类中的全部构造函数

复制代码
 1 //这个例子需要在程序开头添加import java.lang.reflect.*; 2 class hello{ 3     public static void main(String[] args) { 4         Class<?> demo=null; 5         try{ 6             demo=Class.forName("Reflect.Person"); 7         }catch (Exception e) { 8             e.printStackTrace(); 9         }10         Constructor<?>cons[]=demo.getConstructors();11         for (int i = 0; i < cons.length; i++) {12             System.out.println("构造方法:  "+cons[i]);13         }14     }15 }16 //【运行结果】:17 //构造方法:  public Reflect.Person()18 //构造方法:  public Reflect.Person(java.lang.String)
复制代码
复制代码
 1 class hello{ 2     public static void main(String[] args) { 3         Class<?> demo=null; 4         try{ 5             demo=Class.forName("Reflect.Person"); 6         }catch (Exception e) { 7             e.printStackTrace(); 8         } 9         Constructor<?>cons[]=demo.getConstructors();10         for (int i = 0; i < cons.length; i++) {11             Class<?> p[]=cons[i].getParameterTypes();12             System.out.print("构造方法:  ");13             int mo=cons[i].getModifiers();14             System.out.print(Modifier.toString(mo)+" ");15             System.out.print(cons[i].getName());16             System.out.print("(");17             for(int j=0;j<p.length;++j){18                 System.out.print(p[j].getName()+" arg"+i);19                 if(j<p.length-1){20                     System.out.print(",");21                 }22             }23             System.out.println("){}");24         }25     }26 }27 //【运行结果】:28 //构造方法:  public Reflect.Person(){}29 //构造方法:  public Reflect.Person(java.lang.String arg1){}
复制代码

8.取得其他类的全部属性,将这些整理在一起,也就是通过class取得一个类的全部框架

复制代码
 1 class hello { 2     public static void main(String[] args) { 3         Class<?> demo = null; 4         try { 5             demo = Class.forName("Reflect.Person"); 6         } catch (Exception e) { 7             e.printStackTrace(); 8         } 9         System.out.println("===============本类属性========================");10         // 取得本类的全部属性11         Field[] field = demo.getDeclaredFields();12         for (int i = 0; i < field.length; i++) {13             // 权限修饰符14             int mo = field[i].getModifiers();15             String priv = Modifier.toString(mo);16             // 属性类型17             Class<?> type = field[i].getType();18             System.out.println(priv + " " + type.getName() + " "19                     + field[i].getName() + ";");20         }21         System.out.println("===============实现的接口或者父类的属性========================");22         // 取得实现的接口或者父类的属性23         Field[] filed1 = demo.getFields();24         for (int j = 0; j < filed1.length; j++) {25             // 权限修饰符26             int mo = filed1[j].getModifiers();27             String priv = Modifier.toString(mo);28             // 属性类型29             Class<?> type = filed1[j].getType();30             System.out.println(priv + " " + type.getName() + " "31                     + filed1[j].getName() + ";");32         }33     }34 }35 //【运行结果】:36 //===============本类属性========================37 //private java.lang.String sex;38 //===============实现的接口或者父类的属性========================39 //public static final java.lang.String name;40 //public static final int age;
复制代码

9.通过反射调用其他类中的方法

复制代码
 1 class hello {

in(String[] args) { 3 Class<?> demo = null; 4 try { 5 demo = Class.forName("Reflect.Person"); 6 } catch (Exception e) { 7 e.printStackTrace(); 8 } 9 try{10 //调用Person类中的sayChina方法11 Method method=demo.getMethod("sayChina");12 method.invoke(demo.newInstance());13 //调用Person的sayHello方法14 method=demo.getMethod("sayHello", String.class,int.class);15 method.invoke(demo.newInstance(),"Rollen",20);16 }catch (Exception e) {17 e.printStackTrace();18 }19 }20 }21 //【运行结果】:22 //hello ,china23 //Rollen 20
复制代码

10.调用其他类的set和get方法

复制代码
 1 class hello { 2     public static void main(String[] args) { 3         Class<?> demo = null; 4         Object obj=null; 5         try { 6             demo = Class.forName("Reflect.Person"); 7         } catch (Exception e) { 8             e.printStackTrace(); 9         }10         try{11          obj=demo.newInstance();12         }catch (Exception e) {13             e.printStackTrace();14         }15         setter(obj,"Sex","男",String.class);16         getter(obj,"Sex");17     }18 19     /**20      * @param obj   操作的对象21      * @param att   操作的属性22      * */23     public static void getter(Object obj, String att) {24         try {25             Method method = obj.getClass().getMethod("get" + att);26             System.out.println(method.invoke(obj));27         } catch (Exception e) {28             e.printStackTrace();29         }30     }31 32     /**33      * @param obj   操作的对象    34      * @param att   操作的属性35      * @param value 设置的值36      * @param type  参数的属性37      * */38     public static void setter(Object obj, String att, Object value,39             Class<?> type) {40         try {41             Method method = obj.getClass().getMethod("set" + att, type);42             method.invoke(obj, value);43         } catch (Exception e) {44             e.printStackTrace();45         }46     }47 }// end class48 //【运行结果】:49 //
复制代码

11.通过反射操作属性

复制代码
 1 class hello { 2     public static void main(String[] args) throws Exception { 3         Class<?> demo = null; 4         Object obj = null; 5  6         demo = Class.forName("Reflect.Person"); 7         obj = demo.newInstance(); 8  9         Field field = demo.getDeclaredField("sex");10         field.setAccessible(true);11         field.set(obj, "男");12         System.out.println(field.get(obj));13     }14 }// end class
复制代码

12.通过反射取得并修改数组的信息

复制代码
 1 import java.lang.reflect.*; 2  3 class hello{ 4     public static void main(String[] args) { 5         int[] temp={1,2,3,4,5}; 6         Class<?>demo=temp.getClass().getComponentType(); 7         System.out.println("数组类型: "+demo.getName()); 8         System.out.println("数组长度  "+Array.getLength(temp)); 9         System.out.println("数组的第一个元素: "+Array.get(temp, 0));10         Array.set(temp, 0, 100);11         System.out.println("修改之后数组第一个元素为: "+Array.get(temp, 0));12     }13 }14 //【运行结果】:15 //数组类型: int16 //数组长度  517 //数组的第一个元素: 118 //修改之后数组第一个元素为: 100
复制代码

13.通过反射修改数组大小

复制代码
 1 class hello{ 2     public static void main(String[] args) { 3         int[] temp={1,2,3,4,5,6,7,8,9}; 4         int[] newTemp=(int[])arrayInc(temp,15); 5         print(newTemp); 6         System.out.println("====================="); 7         String[] atr={"a","b","c"}; 8         String[] str1=(String[])arrayInc(atr,8); 9         print(str1);10     }11     /**12      * 修改数组大小13      * */14     public static Object arrayInc(Object obj,int len){15         Class<?>arr=obj.getClass().getComponentType();16         Object newArr=Array.newInstance(arr, len);17         int co=Array.getLength(obj);18         System.arraycopy(obj, 0, newArr, 0, co);19         return newArr;20     }21     /**22      * 打印23      * */24     public static void print(Object obj){25         Class<?>c=obj.getClass();26         if(!c.isArray()){27             return;28         }29         System.out.println("数组长度为: "+Array.getLength(obj));30         for (int i = 0; i < Array.getLength(obj); i++) {31             System.out.print(Array.get(obj, i)+" ");32         }33     }34 }35 //【运行结果】:36 //数组长度为: 1537 //1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 =====================38 //数组长度为: 839 //a b c null null null null null
复制代码

14.动态代理

首先来看看如何获得类加载器:

复制代码
 1 class test{ 2  3 } 4 class hello{ 5     public static void main(String[] args) { 6         test t=new test(); 7         System.out.println("类加载器  "+t.getClass().getClassLoader().getClass().getName()); 8     } 9 }10 //【程序输出】:11 //类加载器  sun.misc.Launcher$AppClassLoader
复制代码

其实在java中有三种类类加载器。

1)Bootstrap ClassLoader 此加载器采用c++编写,一般开发中很少见。

2)Extension ClassLoader 用来进行扩展类的加载,一般对应的是jre\lib\ext目录中的类

3)AppClassLoader 加载classpath指定的类,是最常用的加载器。同时也是java中默认的加载器。

如果想要完成动态代理,首先需要定义一个InvocationHandler接口的子类,已完成代理的具体操作。

 View Code

类的生命周期

  在一个类编译完成之后,下一步就需要开始使用类,如果要使用一个类,肯定离不开JVM。在程序执行中JVM通过装载,链接,初始化这3个步骤完成。

  类的装载是通过类加载器完成的,加载器将.class文件的二进制文件装入JVM的方法区,并且在堆区创建描述这个类的java.lang.Class对象。用来封装数据。 但是同一个类只会被类装载器装载以前

链接就是把二进制数据组装为可以运行的状态。

链接分为校验,准备,解析这3个阶段:

校验一般用来确认此二进制文件是否适合当前的JVM(版本),

准备就是为静态成员分配内存空间,。并设置默认值

解析指的是转换常量池中的代码作为直接引用的过程,直到所有的符号引用都可以被运行程序使用(建立完整的对应关系)。

  完成之后,类型也就完成了初始化,初始化之后类的对象就可以正常使用了,直到一个对象不再使用之后,将被垃圾回收。释放空间。当没有任何引用指向Class对象时就会被卸载,结束类的生命周期。

五. IoC原理

Spring中的IoC的实现原理就是工厂模式加反射机制。

1.我们首先看一下不用反射机制时的工厂模式:

复制代码
 1 /** 2  * 工厂模式 3  */ 4 interface fruit{ 5     public abstract void eat(); 6 } 7  8 class Apple implements fruit{ 9     public void eat(){10         System.out.println("Apple");11     }12 }13 14 class Orange implements fruit{15     public void eat(){16         System.out.println("Orange");17     }18 }19 // 构造工厂类20 // 也就是说以后如果我们在添加其他的实例的时候只需要修改工厂类就行了21 class Factory{22     public static fruit getInstance(String fruitName){23         fruit f=null;24         if("Apple".equals(fruitName)){25             f=new Apple();26         }27         if("Orange".equals(fruitName)){28             f=new Orange();29         }30         return f;31     }32 }33 34 class hello{35     public static void main(String[] a){36         fruit f=Factory.getInstance("Orange");37         f.eat();38     }39 }
复制代码

当我们在添加一个子类的时候,就需要修改工厂类了。如果我们添加太多的子类的时候,改的就会很多。

2. 利用反射机制的工厂模式:

复制代码
 1 package Reflect; 2  3 interface fruit{ 4     public abstract void eat(); 5 } 6  7 class Apple implements fruit{ 8     public void eat(){ 9         System.out.println("Apple");10     }11 }12 13 class Orange implements fruit{14     public void eat(){15         System.out.println("Orange");16     }17 }18 19 class Factory{20     public static fruit getInstance(String ClassName){21         fruit f=null;22         try{23             f=(fruit)Class.forName(ClassName).newInstance();24         }catch (Exception e) {25             e.printStackTrace();26         }27         return f;28     }29 }30 31 class hello{32     public static void main(String[] a){33         fruit f=Factory.getInstance("Reflect.Apple");34         if(f!=null){35             f.eat();36         }37     }38 }
复制代码

  现在就算我们添加任意多个子类的时候,工厂类就不需要修改。

  使用反射机制的工厂模式可以通过反射取得接口的实例,但是需要传入完整的包和类名。而且用户也无法知道一个接口有多少个可以使用的子类,所以我们通过属性文件的形式配置所需要的子类。

3.使用反射机制并结合属性文件的工厂模式(即IoC)

首先创建一个fruit.properties的资源文件:

1 apple=Reflect.Apple2 orange=Reflect.Orange

然后编写主类代码:

复制代码
 1 package Reflect; 2  3 import java.io.*; 4 import java.util.*; 5  6 interface fruit{ 7     public abstract void eat(); 8 } 9 10 class Apple implements fruit{11     public void eat(){12         System.out.println("Apple");13     }14 }15 16 class Orange implements fruit{17     public void eat(){18         System.out.println("Orange");19     }20 }21 //操作属性文件类22 class init{23     public static Properties getPro() throws FileNotFoundException, IOException{24         Properties pro=new Properties();25         File f=new File("fruit.properties");26         if(f.exists()){27             pro.load(new FileInputStream(f));28         }else{29             pro.setProperty("apple", "Reflect.Apple");30             pro.setProperty("orange", "Reflect.Orange");31             pro.store(new FileOutputStream(f), "FRUIT CLASS");32         }33         return pro;34     }35 }36 37 class Factory{38     public static fruit getInstance(String ClassName){39         fruit f=null;40         try{41             f=(fruit)Class.forName(ClassName).newInstance();42         }catch (Exception e) {43             e.printStackTrace();44         }45         return f;46     }47 }48 49 class hello{50     public static void main(String[] a) throws FileNotFoundException, IOException{51         Properties pro=init.getPro();52         fruit f=Factory.getInstance(pro.getProperty("apple"));53         if(f!=null){54             f.eat();55         }56     }57 }58 //【运行结果】:Apple
原创粉丝点击