一:委托者

 package com.yeepay.porxy.cglib.test;

 import java.util.HashMap;
import java.util.Map; /**
* 目标类,(委托类)
* @author shangxiaofei
*
*/
public class PaymentServer { public Map<String, String> payMoney(String name,Integer money){
System.out.println("paymentServer.payMoney()付钱=========>名字【"+name+"】,money=【"+money+"】");
Map<String, String> map=new HashMap<String, String>();
map.put("result", "已经完成付钱");
return map;
}
}

二:增强者(代理类)

 package com.yeepay.porxy.cglib.test;

 import java.lang.reflect.Method;

 import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* 增强类
* @author shangxiaofei
*
*/
public class PoxyForService implements MethodInterceptor{ @Override
public Object intercept(Object arg0, Method arg1, Object[] arg2,MethodProxy arg3) throws Throwable {
// 增强功能的代码
System.out.println("PoxyForService.intercept(方法执行前执行========>向统计发送消息)");
if(arg2!=null&&arg2.length>0){
for(int i=0;i<arg2.length;i++){
System.out.println("PoxyForService.intercept()发送的参数内容====>"+arg2[i]);
}
} //执行方法内容
Object object=arg3.invokeSuper(arg0, arg2); System.out.println("PoxyForService.intercept(方法执行后执行=======>计算时间)");
return object;
} }

三:创造代理的工厂类

 package com.yeepay.porxy.cglib.test;

 import net.sf.cglib.proxy.Enhancer;

 /**
* 代理实现工厂类
* @author shangxiaofei
*
*/
public class PoxyFactory {
public static PaymentServer getPaymentServer(){
// Cglib 核心类,生成代理对象
Enhancer enhancer= new Enhancer();
// 为核心类对象中放入需要代理的委托类的类对象
enhancer.setSuperclass(PaymentServer.class);
// 为核心类对象中放入增强功能的类对象
enhancer.setCallback(new PoxyForService());
// 从核心类对象中获取委托类的代理对象
Object object=enhancer.create(); return (PaymentServer) object;
}
}

四:测试类

 package com.yeepay.porxy.cglib.test;

 import java.util.Map;
/**
* 测试类
* @author shangxiaofei
*
*/
public class TestController { private PaymentServer paymentServer=PoxyFactory.getPaymentServer(); public void payment(){
System.out.println("TestController.payment()开始");
Map<String, String> map=paymentServer.payMoney("怪物雷克", 100);
System.out.println("TestController.payment()结束===>"+map.get("result"));
} /**
* TestController.payment()开始
* PoxyForService.intercept(方法执行前执行========>向统计发送消息)
* PoxyForService.intercept()发送的参数内容====>怪物雷克
* PoxyForService.intercept()发送的参数内容====>100
* paymentServer.payMoney()付钱=========>名字【怪物雷克】,money=【100】
* PoxyForService.intercept(方法执行后执行=======>计算时间)
* TestController.payment()结束===>已经完成付钱
* @param args
*/
public static void main(String[] args) {
TestController testController=new TestController();
testController.payment();
}
}

spring源码学习之【准备】cglib动态代理例子的更多相关文章

  1. spring源码学习之路---深入AOP(终)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...

  2. Spring 源码学习——Aop

    Spring 源码学习--Aop 什么是 AOP 以下是百度百科的解释:AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程通过预编译的方式和运行期动态代理实 ...

  3. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  4. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  5. Spring AOP高级——源码实现(1)动态代理技术

    在正式进入Spring AOP的源码实现前,我们需要准备一定的基础也就是面向切面编程的核心——动态代理. 动态代理实际上也是一种结构型的设计模式,JDK中已经为我们准备好了这种设计模式,不过这种JDK ...

  6. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  7. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  8. spring源码学习之路---IOC初探(二)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...

  9. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  10. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

随机推荐

  1. jquery的监听事件和触发事件

    监听事件 $(selector).on('Event me',function(e){ //do something }) 触发事件 $(selector).trigger('Event name') ...

  2. 10 件在 PHP 7 中不要做的事情

    1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅"不应该"使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mysqli_函数 ...

  3. 分享Windows Server 2012 R2的获取正版密钥方法

    然后使用“我有ISIC卡”验证,目前可用号码:S420546009858. 分享Windows Server 2012 R2的获取正版密钥方法. 首先登陆dreamspark注册一个账号https:/ ...

  4. poj3356 dp

    //Accepted 4100 KB 0 ms //类似poj1080 //dp[i][j]表示s1用前i个,s2用前j个的最少匹配步数 //dp[i][j]=min(dp[i][j-1]+1,dp[ ...

  5. POJ 3156 - Interconnect (概率DP+hash)

    题意:给一个图,有些点之间已经连边,现在给每对点之间加边的概率是相同的,问使得整个图连通,加边条数的期望是多少. 此题可以用概率DP+并查集+hash来做. 用dp(i,j,k...)表示当前的每个联 ...

  6. combox源码解析

    /** * jQuery EasyUI 1.3.2 * * Copyright (c) 2009-2013 www.jeasyui.com. All rights reserved. * * Lice ...

  7. Ubuntu 14.10 下卸载MySQL

    前面讲了Mysql的简单安装方式,通过sudo apt-get install mysql-server 等脚本,安装之后如何卸载? 1 通过下面命令删除MySQL sudo apt-get auto ...

  8. resolve some fragment exception

    1.android fragment not attached to activity http://blog.csdn.net/walker02/article/details/7995407 if ...

  9. 2013杭州现场赛B题-Rabbit Kingdom

    杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...

  10. 《软件工程》individual project开发小记(一)

    今天周四没有想去上的课,早八点到中午11点半,下午吃完饭后稍微完善了一下,目前代码可以在dev c++和vs2012上正常运行,性能分析我看资料上一大坨,考虑到目前状态不太好,脑袋转不动了,决定先放一 ...