JDK动态代理和CGLIB动态代理

从反射说起

反射技术应用广泛,使用反射可以实现类的配置化,Spring IoC的基本原理也是如此

在java 中反射是通过包java.lang.reflect.*来实现的

package com.lean.ssm.chapter2.reflect;

public class ReflectServiceImpl {
	public void sayHello(String name){
		System.out.println("Hello "+name);
	}
}

反射生成对象

    public ReflectServiceImpl getInstance(){
		ReflectServiceImpl obj=null;
		try {
			obj=(ReflectServiceImpl) Class
					.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl")
					.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

 

编写测试

    @Test
	public void testSayHello() {
		new ReflectServiceImpl().sayHello("lucas");
		getInstance().sayHello("lucas li");
	}

构建方法含有参数

package com.lean.ssm.chapter2.reflect;

public class ReflectServiceImpl2 {
	
	private String name;
	
	public ReflectServiceImpl2(String name) {
		this.name = name;
	}

	public void sayHello(){
		System.out.println("Hello "+this.name);
	}
}

反射生成对象

    public ReflectServiceImpl2 getInstance2(){
		ReflectServiceImpl2 obj=null;
		try {
			obj = (ReflectServiceImpl2) Class
					.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
					.getConstructor(String.class)
					.newInstance("张三");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

反射方法

    public Object reflectMethod(){
		Object obj=null;
		ReflectServiceImpl target=new ReflectServiceImpl();
		try {
			Method method=ReflectServiceImpl.class
					.getMethod("sayHello", String.class);
			obj=method.invoke(target, "李四");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

反射生成对象和反射调度方法

	/**通过反射,生成的对象,调用的方法,方法的入参都可以实现配置化
	 * @return
	 */
	public Object reflect(){
		ReflectServiceImpl obj=null;
		Object ret=null;
		try {
			obj=(ReflectServiceImpl) Class
					.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl")
					.newInstance();
			Method method=obj.getClass().getMethod("sayHello", String.class);
			ret = method.invoke(obj, "王五");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

动态代理

在java中有多种动态代理技术,比如JDK,CGLIB,Javasisst,ASM,其中最常用的是JDK自带的动态代理和CGLIB(一个第三方库)的动态代理,目前Spring常用JDK和CGLIB,Mybatis还使用了Javassist。

JDK动态代理

jdk的动态代理是包java.lang.reflect.*提供的,它必须借助一个接口来生成代理

package com.lean.ssm.chapter2.reflect;

public interface HelloWorld {
	public void sayHelloWorld();
}

实现类

package com.lean.ssm.chapter2.reflect;

public class HelloWorldImpl implements HelloWorld {

	@Override
	public void sayHelloWorld() {
		System.out.println("Hello World.");
	}

}

代理逻辑

package com.lean.ssm.chapter2.reflect;

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

public class JdkProxyExample implements InvocationHandler{
	
	//真实对象
	private Object target=null;
	public Object bind(Object obj){
		this.target=obj;
		return Proxy.newProxyInstance(
				target.getClass().getClassLoader(),
				target.getClass().getInterfaces(),
				this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("进入代理逻辑方法...");
		System.out.println("在调度真实对象之前的服务");
		Object invoke = method.invoke(target, args);//相当于调用sayHelloWorld方法
		System.out.println("在调用真实对象之后的服务");
		return invoke;
	}

}

测试

	/**
	 * JDK动态代理
	 */
	@Test
	public void testJdkProxy(){
		JdkProxyExample jdk=new JdkProxyExample();
		HelloWorld proxy=(HelloWorld) jdk.bind(new HelloWorldImpl());
		proxy.sayHelloWorld();
	}

	@Test
	public void testSayHello() {
		new ReflectServiceImpl().sayHello("lucas");
		getInstance().sayHello("lucas li");
		
		getInstance2().sayHello();
		reflectMethod();
		reflect();
	}

输出

进入代理逻辑方法...
在调度真实对象之前的服务
Hello World.
在调用真实对象之后的服务

CGLIB动态代理

在一些不能提供接口的环境中,无法使用JDK的动态代理,只能采用其他第三方的技术,比如CGLIB,他的优势在于无需提供接口,只要一个一般的类(非抽象的类)。

package com.lean.ssm.chapter2.reflect;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxyExample implements MethodInterceptor{

	public Object getProxy(Class cls){
		Enhancer enhancer=new Enhancer();
		enhancer.setSuperclass(cls);
		enhancer.setCallback(this);
		return enhancer.create();
	}
	
	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
			throws Throwable {
		System.out.println("在调用真实对象之前");
		Object ret = methodProxy.invokeSuper(proxy, args);
		System.out.println("在调用真实对象之后");
		return ret;
	}

}

测试

	/**
	 * CGLIB动态代理
	 */
	@Test
	public void TestCglibProxy(){
		CglibProxyExample cglib=new CglibProxyExample();
		ReflectServiceImpl proxy = (ReflectServiceImpl) cglib.getProxy(ReflectServiceImpl.class);
		proxy.sayHello("熊二");
	}

输出

在调用真实对象之前
Hello 熊二
在调用真实对象之后

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值