Java笔记之代理

1、静态代理

package staticProxy;

public interface Person {
	public void say();
}

package staticProxy;

public class Chinese implements Person {

	public void say() {
		System.out.println("I am chinese.");

	}

}

package staticProxy;

public class ChineseProxy implements Person {

	private Chinese chinese;

	public ChineseProxy(Chinese chinese) {
		this.chinese = chinese;
	}

	public void say() {

		System.out.println("Before say...");
		chinese.say();
		System.out.println("After say...");

	}

}

package staticProxy;

public class Test {

	
	public static void main(String[] args) {
		Chinese chinese = new Chinese();
		Person p = new ChineseProxy(chinese);
		p.say();
	}
}

 输出:Before say...

I am chinese.

After say...

 

2、JDK动态代理

package dynamicProxy;

public interface Person {
	public void say();
}

package dynamicProxy;

public class Chinese implements Person {

	public void say() {
		System.out.println("I am chinese.");

	}

}

package dynamicProxy;

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

public class DynamicProxy implements InvocationHandler {

	private Object target;

	public Object bind(Object target) {
		this.target = target;

		// 取得代理对象
		// 必须绑定接口,对于没有实现接口的类无法使用,cglib会弥补这个不足
		return Proxy.newProxyInstance(target.getClass().getClassLoader(),
				target.getClass().getInterfaces(), this);
	}

	public Object invoke(Object arg0, Method arg1, Object[] arg2)
			throws Throwable {

		Object result = null;
		System.out.println("Before say...");
		result = arg1.invoke(target, arg2);
		System.out.println("After say...");
		return result;
	}

}

package dynamicProxy;

public class Test {

	public static void main(String[] args) {
		Chinese chinese = new Chinese();
		Person p = (Person) new DynamicProxy().bind(chinese);
		p.say();
	}
}

  输出:Before say...

I am chinese.

After say...

 

3、CGLIB实现的动态代理,不需要实现接口

需要使用委托类的派生出子类,因此,不支持final修饰的最终类

package cglibProxy;

public class Chinese{

	public void say() {
		System.out.println("I am chinese.");

	}

	
	public void eat() {
		System.out.println("Eating...");
		
	}

}

package cglibProxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {

	private Object target;

	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 arg0, Method arg1, Object[] arg2,
			MethodProxy arg3) throws Throwable {
		System.out.println("Before say...");
		arg3.invokeSuper(arg0, arg2);
		System.out.println("After say...");
		return null;
	}

}

package cglibProxy;

public class Test {

	public static void main(String[] args) {
		Chinese chinese = (Chinese) new CglibProxy().getInstance(new Chinese());
		chinese.say();
		chinese.eat();
	}
}

 输出:Before say...

I am chinese.

After say...

Before say...

Eating...

After say...

 

你可能感兴趣的:(java,代理)