Spring学习-Spring06-动态代理

Spring06-动态代理

什么是动态代理?
代理类在程序运行时创建的代理方式被成为 动态代理。 也就是说,这种情
况下,代理类并不是在Java代码中定义的,而是在运行时根据我们在Java代
码中的“指示”动态生成的。

常用的动态代理有两类: JDK动态代理和CGLIB动态代理

两者应用场景:

  1. 如果目标对象实现了接口,采用JDK的动态代理(JDK 提供的代理实现)
  2. 如果目标对象没有实现了接口,必须采用CGLIB动态代理(引入cglib的jar包)
1. JDK动态代理(JDK 提供的代理实现)
package com.caorui.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.caorui.service.SomeService;
import com.caorui.service.impl.SomeServiceImpl;

public class SomeTest {

	public static void main(String[] args) {
		//定义目标对象
		final SomeService target = new SomeServiceImpl();
		//定义目标的代理对象
		SomeService proxy = (SomeService) Proxy.newProxyInstance(target.getClass().getClassLoader(), //目标类的类加载器
													target.getClass().getInterfaces(), //目标类实现的所有接口
													new InvocationHandler() { //调用处理器
														/**
														 * Object proxy, Method method, Object[] args
														 * proxy:代理对象
														 * method:目标方法
														 * args:目标方法的参数
														 */
														@Override
														public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
															String result = (String) method.invoke(target, args);
															return result.toUpperCase();
														}
													});
		
		System.out.println(proxy.doSome());
	}
}


2. CGLIB动态代理(引入cglib的jar包)
  • 创建CGLIB动态代理工厂
package com.caorui.factory;

import java.lang.reflect.Method;

import com.caorui.service.impl.SomeServiceImpl;

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

//cglib动态代理工厂
public class CglibProxyFactory implements MethodInterceptor{
	
	private SomeServiceImpl target;

	public CglibProxyFactory() {
		super();
	}
	public CglibProxyFactory(SomeServiceImpl target) {
		super();
		this.target = target;
	}
	
	//创建cglib代理对象的方法
	public SomeServiceImpl proxyCreator() {
		//创建增强器
		Enhancer enhancer = new Enhancer();
		//指定父类(指定目标类)
		enhancer.setSuperclass(SomeServiceImpl.class);
		//指定回调接口对象
		enhancer.setCallback(this);
		//创建cglib代理对象
		return (SomeServiceImpl) enhancer.create();
		
	}
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		String result = (String) method.invoke(target, args);
		return result.toUpperCase();
	}
	
}
  • 目标类不实现接口
package com.caorui.service.impl;
//目标类
public class SomeServiceImpl {

	public SomeServiceImpl() {
		System.out.println("SomeServiceImpl.SomeServiceImpl()");
	}
	
	public String doSome() {
		return "SomeServiceImpl.doSome()";
	}
}
  • 测试类
package com.caorui.test;

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

import org.junit.Test;

import com.caorui.factory.CglibProxyFactory;

import com.caorui.service.impl.SomeServiceImpl;

public class SomeTest {

	public static void main(String[] args) {
		//定义目标对象
		SomeServiceImpl target = new SomeServiceImpl();
		//定义目标的代理对象
		SomeServiceImpl proxy = new CglibProxyFactory(target).proxyCreator();
		String result = proxy.doSome();
		System.out.println(result);
	}
}

发布了7 篇原创文章 · 获赞 0 · 访问量 174

猜你喜欢

转载自blog.csdn.net/Fu_si/article/details/104523747