java反射详解(4)

动态代理

【案例】首先来看看如何获得类加载器:

1
2
3
4
5
6
7
8
9
class  test{
     
}
class  hello{
     public  static  void  main(String[] args) {
         test t= new  test();
         System.out.println( "类加载器  " +t.getClass().getClassLoader().getClass().getName());
     }
}

【程序输出】:

类加载器  sun.misc.Launcher$AppClassLoader

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

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package  Reflect;
import  java.lang.reflect.*;
 
//定义项目接口
interface  Subject {
     public  String say(String name,  int  age);
}
 
// 定义真实项目
class  RealSubject  implements  Subject {
     @Override
     public  String say(String name,  int  age) {
         return  name +  "  "  + age;
     }
}
 
class  MyInvocationHandler  implements  InvocationHandler {
     private  Object obj =  null ;
 
     public  Object bind(Object obj) {
         this .obj = obj;
         return  Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
                 .getClass().getInterfaces(),  this );
     }
 
     @Override
     public  Object invoke(Object proxy, Method method, Object[] args)
             throws  Throwable {
         Object temp = method.invoke( this .obj, args);
         return  temp;
     }
}
 
class  hello {
     public  static  void  main(String[] args) {
         MyInvocationHandler demo =  new  MyInvocationHandler();
         Subject sub = (Subject) demo.bind( new  RealSubject());
         String info = sub.say( "Rollen" 20 );
         System.out.println(info);
     }
}

【运行结果】:

Rollen  20

类的生命周期

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

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

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

 

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

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

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

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

完成之后,类型也就完成了初始化,初始化之后类的对象就可以正常使用了,直到一个对象不再使用之后,将被垃圾回收。释放空间。

当没有任何引用指向Class对象时就会被卸载,结束类的生命周期

将反射用于工厂模式

先来看看,如果不用反射的时候,的工厂模式吧:

http://www.cnblogs.com/rollenholt/archive/2011/08/18/2144851.html

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

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

现在我们看看利用反射机制:

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

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

 

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

下面我们来看看: 结合属性文件的工厂模式

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

内容为:

1
2
apple=Reflect.Apple
orange=Reflect.Orange

 然后编写主类代码:

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

【运行结果】:Apple

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值