动态代理
与静态代理类对照的是动态代理类,动态代理类的字节码在程序运行时由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("");
}
}
与静态代理类对照的是动态代理类,动态代理类的字节码在程序运行时由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("");
}
}