您的位置:首页 > 编程语言 > Java开发

java 动态代理

2016-06-17 11:52 417 查看
1.java 动态代理实例:
package com.xp.dsp.dynamicProxy;

/**
* Created by Skyline on 2016/6/16.
*/
public interface Subject {

void doSomeThing();

}
package com.xp.dsp.dynamicProxy;

/**
* Created by Skyline on 2016/6/16.
*/
public class SubjectImpl implements Subject {

private String name = "subjectImpl";

@Override
public void doSomeThing() {
System.out.println("run doSomeThing>>>>>>");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
package com.xp.dsp.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
* 调用处理器
* 该处理器定义代理的方法:执行被代理对象方法前后添加行为
* Created by Skyline on 2016/6/16.
*/
public class ProxyHandler implements InvocationHandler {

//被代理对象
private Object obj;

/**
* 代理obj,返回代理对象
* @param obj   被代理对象
* @return
*/
public Object proxy(Object obj) {
this.obj = obj;
/**
* 生成代理对象
* 该代理对象内部拥有该调用处理器ProxyHandler的实例:this
*/
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);
}

/**
* 代理对象执行方法时其实是委托给代理对象内部的InvocationHandler实例方法invoke
* @param proxy     代理对象
* @param method    执行的方法
* @param args      执行的方法参数
* @return
* @throws Throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

System.out.println("before calling: " + method.getName());
method.invoke(obj, args);
System.out.println("after calling: " + method.getName());
return null;
}
}
package com.xp.dsp.dynamicProxy;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
* Created by Skyline on 2016/6/16.
*/
public class Test {

public static void main(String[] args) {
ProxyHandler proxyHandler = new ProxyHandler();
Subject proxyInstance = (Subject)proxyHandler.proxy(new SubjectImpl());

System.out.println("proxyInstance class name: " + proxyInstance.getClass().getName());

Class<?>[] interfaces = proxyInstance.getClass().getInterfaces();
for (Class<?> anInterface : interfaces) {
System.out.println("interface:" + anInterface.getName());
}

Method[] methods = proxyInstance.getClass().getMethods();
for (Method method : methods) {
System.out.println("method:"+method.getName());method.getName();
}

Field[] fields = proxyInstance.getClass().getDeclaredFields();
for (Field field : fields) {
System.out.println("field:" + field.getName());
}

proxyInstance.doSomeThing();
proxyInstance.toString();
proxyInstance.equals("");
}

}
执行结果:
proxyInstance class name: com.sun.proxy.$Proxy0
interface:com.xp.dsp.dynamicProxy.Subject
method:equals
method:toString
method:hashCode
method:doSomeThing
method:isProxyClass
method:getInvocationHandler
method:getProxyClass
method:newProxyInstance
method:wait
method:wait
method:wait
method:getClass
method:notify
method:notifyAll
field:m1
field:m2
field:m3
field:m0
before calling: doSomeThing
run doSomeThing>>>>>>
after calling: doSomeThing
before calling: toString
after calling: toString
2. Cglib代理
实例:
package com.xp.dsp.cglib;/*** Created by Skyline on 2016/6/16.*/public class SubjectImpl{private String name = "subjectImpl";public void doSomeThing() {System.out.println("run doSomeThing>>>>>>");}public String getName() {return name;}public void setName(String name) {this.name = name;}}
package com.xp.dsp.cglib;//import org.springframework.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/*** 采用cglib代理** Created by Skyline on 2016/6/17.*/public class CustomCglib implements MethodInterceptor {//被代理对象private Object obj;/*** 代理obj,返回代理对象* @param obj 被代理对象* @return*/public Object proxy(Object obj) {this.obj = obj;Enhancer enhancer = new Enhancer();enhancer.setSuperclass(this.obj.getClass());// 回调方法,即代理对象执行方法是会调用内部的回掉实例(CustomCglib的实例)的intercept方法。enhancer.setCallback(this);// 创建代理对象return enhancer.create();}@Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {System.out.println("before calling: " + method.getName());//调用methodProxy.invokeSuper(obj, args);System.out.println("after calling: " + method.getName());return null;}}
package com.xp.dsp.cglib;import java.lang.reflect.Method;/*** Created by Skyline on 2016/6/16.*/public class Test {public static void main(String[] args) {CustomCglib cglib = new CustomCglib();SubjectImpl proxyInstance = (SubjectImpl)cglib.proxy(new SubjectImpl());System.out.println("proxyInstance class name: " + proxyInstance.getClass().getName());Class<?>[] interfaces = proxyInstance.getClass().getInterfaces();for (Class<?> anInterface : interfaces) {System.out.println("interface:" + anInterface.getName());}proxyInstance.doSomeThing();proxyInstance.toString();}}
执行结果:
proxyInstance class name: com.xp.dsp.cglib.SubjectImpl$$EnhancerByCGLIB$$edfb19c4interface:net.sf.cglib.proxy.Factorybefore calling: doSomeThingrun doSomeThing>>>>>>after calling: doSomeThingbefore calling: toStringDisconnected from the target VM, address: '127.0.0.1:54586', transport: 'socket'before calling: hashCodeafter calling: hashCodeafter calling: toString

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 代理