Spring---动态代理

动态代理

代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控
制对某个对象的访问。代理类负责为委托类预处理消息,过滤消息并转发
消息,以及进行消息被委托类执行后的后续处理。
代理类在程序运行时创建的代理方式被成为 动态代理。 也就是说,这
种情况下,代理类并不是在 Java 代码中定义的,而是在运行时根据我们在
Java 代码中的“指示”动态生成的。相比于静态代理, 动态代理的优势在于
可以很方便的对代理类的函数进行统一的处理,而不用修改每个代理类的
函数

1) 接口方式
代理类和目标类共同实现相同的接口,userService 为接口,UserService
为抽象类

package com.hadwinling.jdk.client;

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

/*
 * 演示JDK动态代理实现
 * 案例:
 * 	真实角色:
 *  演员
 *  代理角色:
 *  经纪公司
 *  客户:剧组
 *  剧组通过经纪公司找演员  出演电影
 */
public class Client {
	public static void main(String[] args) {
		Actor actor = new Actor();
		/**
		 * ClassLoader:和被代理对象使用相同的类加载器。 Interfaces:和被代理对象具有相同的行为。实现相同的接口
		 * InvocationHandler:如何代理
		 */
		// 获取代理对象
		IActor proxyActor = (IActor) Proxy.newProxyInstance(Actor.class.getClassLoader(), Actor.class.getInterfaces(),
				new InvocationHandler() {
					/**
					 * 执行被代理对象的任何方法,都会经过该方法。 此方法有拦截的功能。 可以拦截所有被代理对象的方法,对方法增强 proxy:被代理对象的引用
					 * method:要增强的方法 args:要增强的方法的参数 返回值: 当前执行方法的返回值
					 * 
					 */
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						// TODO Auto-generated method stub
						String name = method.getName();
						System.out.println("被拦截到的方法名:" + name);
						// 获取参数
						int money = (int) args[0];
						// 对方法增强
						method.invoke(actor, money / 2);
						return null;
					}
				});
		// 出品方找经纪公司
		proxyActor.play(100000);

	}

}

//接口  代理角色   经纪公司
interface IActor {
	void play(int money);
}

//真实角色  演员
class Actor implements IActor {
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gem.demo.jdk.proxy.IActor#play(int)
	 */
	@Override
	public void play(int money) {
		// TODO Auto-generated method stub
		System.out.println("演员得到的薪酬:" + money);
	}

}

2) CGLIB 介绍
没有接口,创建子类为代理对象,使用cglib需要导包,
在这里插入图片描述

package com.hadwinling.cglib.client;

import java.lang.reflect.Method;

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

/*
 * 基于类实现动态代理
 */
public class Client {
	public static void main(String[] args) {
		//剧组需要找演员
		Actor a = new Actor();
		//获取代理对象
		/**
		 * type:
		 * callback
		 */
		Actor actor = (Actor) Enhancer.create(Actor.class,
				new MethodInterceptor() {
			//拦截被代理对象的方法
			/**
			 * arg0	arg1 arg2 与jdk的动态代理的含义一样
			 * arg3:方法代理对象
			 *
			 */
			@Override
			public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
				// TODO Auto-generated method stub
				//获取方法名:
				System.out.println("被拦截的方法:"+arg1.getName());
				//获取参数
				int money = (int)arg2[0];
				//增强方法
				arg1.invoke(a, money/2);
				return null;
			}
		});
		//代理角色执行
		actor.proxyMethod(20000);
	}

}
class Actor{
	public void proxyMethod(int money) {
		System.out.println("演员出演该剧,获得的酬劳:"+money);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值