摘要笔记:代理实现了方法的扩充。代理都要实现接口,static--被代理类的接口;
dynamic--InvocationHandler,实现invoke方法创建对象object传给method.invoke(object,orgs)--执行代理方法;
CGLIB--MethodInterceptor,创建对象object并重写intercept方法,通过method.invoke--执行代理。
接口
//Person.java
1 package heqing.spring.proxy;
2
3 public interface Person {
4 public void say();
5 }
具体类
//Student.java
1 package heqing.spring.proxy;
2
3 public class Student implements Person {
4 private String sname;
5 public Student() {
6 sname="Student";
7 }
8 public void say() {
9 System.out.println("My name is "+sname+",I'm a student");
10 }
11 }
//Teacher.java
package heqing.spring.proxy;
1 public class Teacher implements Person {
2 private String tname;
3 public Teacher() {
4 tname="teacher";
5 }
6 public void say() {
7 System.out.println("My name is "+tname+",I'm a teacher");
8 }
9 }
静态代理,动态代理,CGLIB代理
//StaticProxy
1 package heqing.spring.proxy;
2
3 public class StaticProxy implements Person {
4 private Person person;
5 public StaticProxy(Person person) {
6 this.person=person;
7 }
8 @Override
9 public void say() {
10 System.out.println("I'm StaticProxy.");
11 person.say();
12 System.out.println("I'm over Excute");
13 }
14 }
//DynamicProxy.java
1 package heqing.spring.proxy;
2
3 import java.lang.reflect.InvocationHandler;
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Proxy;
6
7 public class DynamicProxy implements InvocationHandler {
8 private Object obj;
9 public Object createInstance(Object obj){
10 this.obj=obj;
11 return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
12 }
13
14 @Override
15 public Object invoke(Object proxy, Method method, Object[] args)
16 throws Throwable {
17 System.out.println("I'm DynamicProxy");
18 Object result=method.invoke(obj, args);
19 return null;
20 }
21 }
//CGLIBProxy.java
1 package heqing.spring.proxy;
2
3 import java.lang.reflect.Method;
4
5 import net.sf.cglib.proxy.Enhancer;
6 import net.sf.cglib.proxy.MethodInterceptor;
7 import net.sf.cglib.proxy.MethodProxy;
8
9 public class CGLIBProxy implements MethodInterceptor {
10 Object obj;
11 public Object createInstance(Object object){
12 obj=object;
13 Enhancer enchaner=new Enhancer();
14 enchaner.setSuperclass(obj.getClass());
15 enchaner.setCallback(this);
16 return enchaner.create();
17 }
18 @Override
19 public Object intercept(Object Proxy, Method method, Object[] args,
20 MethodProxy methodProxy) throws Throwable {
21 System.out.println("I'm MethodInterceptor");
22 Object result=method.invoke(obj, args);
23 System.out.println(method.getName());
24 return result;
25 }
26 }
测试类
1 //TestStaticProxy.java
2 package heqing.spring.proxy;
3
4 public class TestStaticProxy {
5 public static void main(String[] args) {
6 StaticProxy sp=new StaticProxy(new Student());
7 sp.say();
8 }
9 }
1 //TestDynamicProxy.java
2 package heqing.spring.proxy;
3
4 public class TestDynamicProxy {
5 public static void main(String[] args) {
6 DynamicProxy dynamicProxy=new DynamicProxy();
7 Person t=(Person)dynamicProxy.createInstance(new Student());
8 t.say();
9 }
10 }
1 //TestCGLIBProxy.java
2 package heqing.spring.proxy;
3
4 public class TestCGLIBProxy {
5 public static void main(String[] args) {
6 CGLIBProxy proxy=new CGLIBProxy();
7 Person person=(Person)proxy.createInstance(new Teacher());
8 person.say();
9 }
11 }