Java 代理机制

2017-09-13 10:33:39来源:网络收集作者:纳米程序员人点击

分享

学习心得:

相关问题探索:


关于静态代理和动态代理:

静态代理和动态代理问题是一个很重要也很难准确理解的定义。


静态代理:

静态代理是指 在程序运行之前就已经存在代理类的字节码文件,代理类和委托类的关系在运行前就已经确定了。


缺陷: 一个接口只能服务一种类型的对象,当代理的方法很多时,很难扩展


动态代理:

在程序运行期间由JVM根据反射机制动态生成。


package cglib;
/**
* Created by panyunyi on 2017/9/11.
* CUFE cs14
*/
public interface Hello {
void say(String name);
}
package cglib;
/**
* Created by panyunyi on 2017/9/11.
* CUFE cs14
*/
public class HelloImpl implements Hello{
@Override
public void say(String name) {
System.out.println("Hello"+name);
}
}
package cglib;
/**
* Created by panyunyi on 2017/9/11.
* CUFE cs14
*/
public class HelloProxy implements Hello{
private Hello hello;
public HelloProxy(){
hello=new HelloImpl();
}
@Override
public void say(String name) {
before();
hello.say(name);
after();
}
private void after() {
System.out.println("after");
}
private void before() {
System.out.println("before");
}
}
package cglib;
import Java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created by panyunyi on 2017/9/11.
* CUFE cs14
*/
public class DynamicProxy implements InvocationHandler {
private Object target;
public DynamicProxy(Object object){
this.target=object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object result=method.invoke(target,args);
after();
return result;
}
private void after() {
System.out.println("after");
}
private void before() {
System.out.println("before");
}
/*
* 做一个简单封装
* */
@SuppressWarnings("uncheck")
public T getProxy(){
return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
}
package cglib;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* Created by panyunyi on 2017/9/11.
* CUFE cs14
*/
public class CGLibProxy implements MethodInterceptor {
public T getProxy(Class cls){
return (T) Enhancer.create(cls,this);
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
before();
Object result= methodProxy.invokeSuper(o,objects);
after();
return result;
}
private void after() {
System.out.println("after");
}
private void before() {
System.out.println("before");
}
}
package cglib;
import java.lang.reflect.Proxy;
/**
* Created by panyunyi on 2017/9/11.
* CUFE cs14
*
*
*/
public class testMain {
public static void main(String args[]){
staticProxy();
dynamicProxy();
cglibProxy();
}
private static void cglibProxy() {
CGLibProxy cgLibProxy=new CGLibProxy();
Hello helloProxy=cgLibProxy.getProxy(HelloImpl.class);
helloProxy.say("Jack");
}
private static void dynamicProxy() {
Hello hello=new HelloImpl();
DynamicProxy dynamicProxy=new DynamicProxy(hello);
Hello helloProxy=(Hello) Proxy.newProxyInstance(
hello.getClass().getClassLoader(),
hello.getClass().getInterfaces(),
dynamicProxy
);
helloProxy.say("真的是傻逼");
}
private static void staticProxy() {
Hello helloProxy=new HelloProxy();
helloProxy.say("sb");
}
}

附上相关代码仅供参考。




最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台