spring源码学习之【准备】cglib动态代理例子
一:委托者
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动态代理例子的更多相关文章
- spring源码学习之路---深入AOP(终)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...
- Spring 源码学习——Aop
Spring 源码学习--Aop 什么是 AOP 以下是百度百科的解释:AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程通过预编译的方式和运行期动态代理实 ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
- Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点
Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...
- Spring AOP高级——源码实现(1)动态代理技术
在正式进入Spring AOP的源码实现前,我们需要准备一定的基础也就是面向切面编程的核心——动态代理. 动态代理实际上也是一种结构型的设计模式,JDK中已经为我们准备好了这种设计模式,不过这种JDK ...
- Spring源码学习笔记9——构造器注入及其循环依赖
Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...
- Spring 源码学习笔记11——Spring事务
Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...
- spring源码学习之路---IOC初探(二)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...
- Spring源码学习
Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...
- Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md
写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...
随机推荐
- [安卓][转]internal(com.android.internal)和hidden(@hide)APIs简介及在应用程序中的调用方法
转自:http://www.cnblogs.com/xirihanlin/archive/2011/06/05/2073118.html [引言]:我在做android softap的时候看到andr ...
- Unity4.3.3激活
Unity4.X Win版本的破解方法: <ignore_js_op> 1.安装unity4.X,一路按提示下一步,要断网,直到激活运行软件后再联网2.将Unity 4.x Pro Pat ...
- Android 自带图标库 android.R.drawable
在xml文件中调用. android:title="@string/secure_connect"android:orderInCategory="100"an ...
- 转:HashMap深度解析(一)
HashMap哈希码hashCodeequals 本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/16843543,转载 ...
- FZU1683 矩阵
//Accepted 220 KB 359 ms #include <cstdio> #include <cstring> ; int pp; struct matrix { ...
- hdu 2000
ps:刚开始学C++...用C++来试试.. 代码: #include <iostream> using namespace std; int main(){ ],t,i,j; ]> ...
- alloc和初始化的定义
1.alloc是为原始实例进行分配内存,但是还不能使用 2.初始化的作用就是将一个对象的初始状态(即它的实例变量和属性)设定为合理的值,然后返回对象.它的目的就是返回一个有用的值
- 解决:insert Vodafone sim card,open the mms read report,when receive the read report,cann't download..
insert Vodafone sim card,open the mms read report,when receive the read report,cann't download the m ...
- hdu - 2083 - 简易版之最短距离
找到中位数 , 根据对称性 , 当中位数需要两个数取中值的时候不需要取 , 只需要其中的任意一个数几个 例如四个数 1 , 2 , 3 , 4 . 这四个数 , 其中的 2 和 3 都可以 . 然后求 ...
- 【LeetCode OJ】Word Break
Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...