很少有时间静下心来总结技术,今天就打算把代理 1 静态 2 动态 搞个明白!
静态代理企业很少用,但是可以为我们理解动态代理奠定逻辑思路基础。
a 一个接口A b 一个实现类B(被代理的类) c 一个静态代理类C
其中 class B implements A
class C implements A
也就是说,被代理类(目标类)实现了哪些接口,我这个静态代理类也就实现多少接口。
那为什么要实现那么多接口呢?
这个要从java 的继承机制说起了, 实现了接口就实现了接口中的所有方法。
代理类如果仅仅实现了一种接口,被代理类中的其他方法就不能被调用,也就不能说我代理了这个类!而只能说我代理了来自这个接口的方法。
静态代理
接口
package com.liujl;
public interface Hello {
public abstract void hello(String say);
}
目标类 (被代理的类)
package com.liujl.impl;
import com.liujl.Hello;
public class HelloSpecker implements Hello {
@Override
public void hello(String say) {
System.out.println("subClass----targetClass say : "+say);
}
}
代理类
package com.liujl.static_proxy;
import com.liujl.Hello;
import com.liujl.impl.HelloSpecker;
public class StaticProxy implements Hello{
private HelloSpecker helloSpecker;
public StaticProxy() {
}
public StaticProxy(HelloSpecker helloSpecker) {
super();
this.helloSpecker = helloSpecker;
}
@Override
public void hello(String say) {
System.out.println("执行前");
helloSpecker.hello(say);
System.out.println("执行后");
}
}
测试类 使用junit4
package com.liujl.test;
import org.junit.Test;
import com.liujl.Hello;
import com.liujl.impl.HelloSpecker;
import com.liujl.static_proxy.StaticProxy;
public class StaticProxyTest {
@Test
public void test() {
Hello hello=new StaticProxy(new HelloSpecker());
hello.hello("hello123");
}
}
JDK动态代理
接口
package com.liujl;
public interface PersonService {
public abstract void save(String name);
}
package com.liujl.impl;
import com.liujl.PersonService;
public class PersonServiceBean implements PersonService {
private String user;
public PersonServiceBean() { }
public PersonServiceBean(String user) {
this.user = user;
}
@Override
public void save(String name) {
System.out.println("save()方法被执行 name= "+name);
}
public String getUser() {
return user;
}
}
JDK动态代理
package com.liujl.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.liujl.impl.PersonServiceBean;
public class JDKProxyFactory implements InvocationHandler {
private Object targetObject;
public Object createProxyObject(Object targetObject){
this.targetObject=targetObject;
return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),
this.targetObject.getClass().getInterfaces(), this);
//根据被代理对象,创建代理对象(实现了被代理对象的所有接口),
//this代表回调对象,当代理对象所代理的方法被执行时,调用本类的invoke方法进行拦截
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
PersonServiceBean bean=(PersonServiceBean)this.targetObject;
Object result=null;
if(bean.getUser()!=null){//进行了权限拦截
result= method.invoke(targetObject, args);
}
return result;
}
}
使用JUnit4 进行单元测试
package juint.test;
import org.junit.Test;
import com.liujl.PersonService;
import com.liujl.impl.PersonServiceBean;
import com.liujl.proxy.JDKProxyFactory;
public class ProxyTest {
@Test
public void test() {
JDKProxyFactory proxy=new JDKProxyFactory();
PersonService service=(PersonService) proxy.createProxyObject(new PersonServiceBean());
service.save("我是被代理对象");
}
}
写的很浅,班门弄斧了,请多提宝贵建议,相互学习。