JAVA Proxy(代理) 学习

动态代理 
与静态代理类对照的是动态代理类,动态代理类的字节码在程序运行时由Java反射机制动态生成,无需程序员手工编写它的源代码。动态代理类不仅简化了编程工作,而且提高了软件系统的可扩展性,因为Java 反射机制可以生成任意类型的动态代理类。java.lang.reflect 包中的Proxy类和InvocationHandler 接口提供了生成动态代理类的能力。

1、JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理

接口定义
public interface BusInterface {
public void doSomething(String name);
}
接口实现
public class BusInterfaceImpl implements BusInterface {
@Override
public void doSomething(String name) {
System.out.println(name + " doSomething");
}
}


import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class BusProxy implements InvocationHandler {
private Object obj;

public BusProxy() {
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
result = method.invoke(obj, args);
return result;
}

public Object factory(Object obj) {
this.obj = obj;
Class<? extends Object> cls = obj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), this);
}

}


测试:
public class TestProxy {
public static void main(String[] args) {
BusInterface bus = (BusInterface) new BusProxy().factory(new BusInterfaceImpl());
bus.doSomething("derek");
}
}


Cglib动态代理 
cglib是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继承,所以不能对final修饰的类进行代理。 

cglib-2.2.2.jar、asm-3.0.jar

public class BusImpl{
@Writer
public void doSomething(String name) {
System.out.println(name + " doSomething");
}
}

package com.chj.porxy;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.commons.lang.StringUtils;
import com.chj.annotation.ReadOnly;
import com.chj.annotation.Writer;

public class CgLibProxy implements MethodInterceptor {

private Object target;

/**
* 创建代理对象

* @param target
* @return
*/
public Object getInstance(Object target) {
this.target = target;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.target.getClass());
// 回调方法
enhancer.setCallback(this);
// 创建代理对象
return enhancer.create();
}

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

Object result = null;
String arg = (String) args[0];

if (method.isAnnotationPresent(ReadOnly.class)) {
Annotation ann_method = method.getAnnotation(ReadOnly.class);
if (StringUtils.isEmpty(arg)) {
arg = ((ReadOnly) ann_method).value();
}
}

if (method.isAnnotationPresent(Writer.class)) {
Annotation ann_method = method.getAnnotation(Writer.class);
if (StringUtils.isEmpty(arg)) {
arg = ((Writer) ann_method).value();
}
}

args[0] = arg;
result = proxy.invokeSuper(obj, args);

return result;
}
}


测试:
public class TestProxy {
public static void main(String[] args) {
CgLibProxy cglib=new CgLibProxy();  
BusImpl bus=(BusImpl)cglib.getInstance(new BusImpl());
bus.doSomething("");
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值