Java动态代理的应用详解
动态代理其实就是java.lang.reflect.proxy类动态的根据您指定的所有接口生成一个class byte,该class会继承proxy类,并实现所有你指定的接口(您在参数中传入的接口数组);然后再利用您指定的classloader将 class byte加载进系统,最后生成这样一个类的对象,并初始化该对象的一些值,如invocationhandler,以即所有的接口对应的method成员。 初始化之后将对象返回给调用的客户端。这样客户端拿到的就是一个实现你所有的接口的proxy对象。请看实例分析:
package com.fans.common.proxy;
public interface businessprocessor {
public void processbusiness();
}
package com.fans.common.proxy;
/**
* 业务处理类
* @author fanshadoop
*
*/
public class businessprocessorimpl implements businessprocessor {
@override
public void processbusiness() {
system.out.println("processing business.....");
}
}
package com.fans.common.proxy;
import java.lang.reflect.invocationhandler;
import java.lang.reflect.method;
/**
* 业务代理类
* @author fanshadoop
*
*/
public class businessprocessorhandler implements invocationhandler {
private object target = null;
businessprocessorhandler(object target) {
this.target = target;
}
public object invoke(object proxy, method method, object[] args)
throws throwable {
system.out
.println("you can do something here before process your business");
object result = method.invoke(target, args);
system.out
.println("you can do something here after process your business");
return result;
}
}
package com.fans.common.proxy;
import java.lang.reflect.field;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import java.lang.reflect.proxy;
public class test {
/**
* @param args
*/
public static void main(string[] args) {
businessprocessorimpl bpimpl = new businessprocessorimpl();
businessprocessorhandler handler = new businessprocessorhandler(bpimpl);
businessprocessor bp = (businessprocessor) proxy.newproxyinstance(
bpimpl.getclass().getclassloader(), bpimpl.getclass()
.getinterfaces(), handler);
bp.processbusiness();
system.out.println(bp.getclass().getname());
printclassdefinition(bp.getclass());
}
public static string getmodifier(int modifier) {
string result = "";
switch (modifier) {
case modifier.private:
result = "private";
case modifier.public:
result = "public";
case modifier.protected:
result = "protected";
case modifier.abstract:
result = "abstract";
case modifier.final:
result = "final";
case modifier.native:
result = "native";
case modifier.static:
result = "static";
case modifier.synchronized:
result = "synchronized";
case modifier.strict:
result = "strict";
case modifier.transient:
result = "transient";
case modifier.volatile:
result = "volatile";
case modifier.interface:
result = "interface";
}
return result;
}
public static void printclassdefinition(class clz) {
string clzmodifier = getmodifier(clz.getmodifiers());
if (clzmodifier != null && !clzmodifier.equals("")) {
clzmodifier = clzmodifier + " ";
}
string superclz = clz.getsuperclass().getname();
if (superclz != null && !superclz.equals("")) {
superclz = "extends " + superclz;
}
class[] interfaces = clz.getinterfaces();
string inters = "";
for (int i = 0; i < interfaces.length; i++) {
if (i == 0) {
inters += "implements ";
}
inters += interfaces[i].getname();
}
system.out.println(clzmodifier + clz.getname() + " " + superclz + " "
+ inters);
system.out.println("{");
field[] fields = clz.getdeclaredfields();
for (int i = 0; i < fields.length; i++) {
string modifier = getmodifier(fields[i].getmodifiers());
if (modifier != null && !modifier.equals("")) {
modifier = modifier + " ";
}
string fieldname = fields[i].getname();
string fieldtype = fields[i].gettype().getname();
system.out.println(" " + modifier + fieldtype + " " + fieldname
+ ";");
}
system.out.println();
method[] methods = clz.getdeclaredmethods();
for (int i = 0; i < methods.length; i++) {
method method = methods[i];
string modifier = getmodifier(method.getmodifiers());
if (modifier != null && !modifier.equals("")) {
modifier = modifier + " ";
}
string methodname = method.getname();
class returnclz = method.getreturntype();
string retruntype = returnclz.getname();
class[] clzs = method.getparametertypes();
string paralist = "(";
for (int j = 0; j < clzs.length; j++) {
paralist += clzs[j].getname();
if (j != clzs.length - 1) {
paralist += ", ";
}
}
paralist += ")";
clzs = method.getexceptiontypes();
string exceptions = "";
for (int j = 0; j < clzs.length; j++) {
if (j == 0) {
exceptions += "throws ";
}
exceptions += clzs[j].getname();
if (j != clzs.length - 1) {
exceptions += ", ";
}
}
exceptions += ";";
string methodprototype = modifier + retruntype + " " + methodname
+ paralist + exceptions;
system.out.println(" " + methodprototype);
}
system.out.println("}");
}
}
运行结果:
you can do something here before process your business
processing business.....
you can do something here after process your business
$proxy0
$proxy0 extends java.lang.reflect.proxy implements com.fans.common.proxy.businessprocessor
{
java.lang.reflect.method m1;
java.lang.reflect.method m3;
java.lang.reflect.method m0;
java.lang.reflect.method m2;
boolean equals(java.lang.object);
java.lang.string tostring();
int hashcode();
void processbusiness();
}
类businessprocessorhandler实现了invocationhandler接口的invoke方法,这个类就是proxy最终调用固定接口方法。
很明显,proxy.newproxyinstance方法会做如下几件事:
1,根据传入的第二个参数interfaces动态生成一个类,实现interfaces中的接口,该例中即businessprocessor接口的processbusiness方法。并且继承了proxy类,重写了hashcode,tostring,equals等三个方法。具体实现可参看 proxygenerator.generateproxyclass(...); 该例中生成了$proxy0类
2,通过传入的第一个参数classloder将刚生成的类加载到jvm中。即将$proxy0类load
3,利用第三个参数,调用$proxy0的$proxy0(invocationhandler)构造函数 创建$proxy0的对象,并且用interfaces参数遍历其所有接口的方法,并生成method对象初始化对象的几个method成员变量
4,将$proxy0的实例返回给客户端。
现在好了。我们再看客户端怎么调就清楚了。
1,客户端拿到的是$proxy0的实例对象,由于$proxy0继承了businessprocessor,因此转化为businessprocessor没任何问题。
businessprocessor bp = (businessprocessor)proxy.newproxyinstance(....);
2,bp.processbusiness();
实际上调用的是$proxy0.processbusiness();那么$proxy0.processbusiness()的实现就是通过invocationhandler去调用invoke方法啦!