没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Spring 学习总结(二)——静态代理、JDK
与 CGLIB 动态代理、AOP+IoC
目录
� 一、为什么需要代理模式
� 二、静态代理
� 三、动态代理,使用 JDK 内置的 Proxy 实现
� 四、动态代理,使用 cglib 实现
� 五、使用 Spring 实现 AOP
� 六、使用 IOC 配置的方式实现 AOP
� 七、使用 XML 配置 Spring AOP 切面
� 八、示例下载
一、为什么需要代理模式
假设需实现一个计算的类 Math、完成加、减、乘、除功能,如下所示:
1 package com.zhangguo.Spring041.aop01;
2
3 public class Math {
4 //加
5 public int add(int n1,int n2){
6 int result=n1+n2;
7 System.out.println(n1+"+"+n2+"="+result);
8 return result;
9 }
10
11
12 //减
13 public int sub(int n1,int n2){
14 int result=n1-n2;
15 System.out.println(n1+"-"+n2+"="+result);
16 return result;
17 }
18
19 //乘
20 public int mut(int n1,int n2){
21 int result=n1*n2;
22 System.out.println(n1+"X"+n2+"="+result);
23 return result;
24 }
25
26 //除
27 public int div(int n1,int n2){
28 int result=n1/n2;
29 System.out.println(n1+"/"+n2+"="+result);
30 return result;
31 }
32 }
现在需求发生了变化,要求项目中所有的类在执行方法时输出执行耗时。最直接的办法是
修改源代码,如下所示:
1 package com.zhangguo.Spring041.aop01;
2
3 import java.util.Random;
4
5 public class Math {
6 //加
7 public int add(int n1,int n2){
8 //开始时间
9 long start=System.currentTimeMillis();
10 lazy();
11 int result=n1+n2;
12 System.out.println(n1+"+"+n2+"="+result);
13 Long span= System.currentTimeMillis()-start;
14 System.out.println("共用时:"+span);
15 return result;
16 }
17
18 //减
19 public int sub(int n1,int n2){
20 //开始时间
21 long start=System.currentTimeMillis();
22 lazy();
23 int result=n1-n2;
24 System.out.println(n1+"-"+n2+"="+result);
25 Long span= System.currentTimeMillis()-start;
26 System.out.println("共用时:"+span);
27 return result;
28 }
29
30 //乘
31 public int mut(int n1,int n2){
32 //开始时间
33 long start=System.currentTimeMillis();
34 lazy();
35 int result=n1*n2;
36 System.out.println(n1+"X"+n2+"="+result);
37 Long span= System.currentTimeMillis()-start;
38 System.out.println("共用时:"+span);
39 return result;
40 }
41
42 //除
43 public int div(int n1,int n2){
44 //开始时间
45 long start=System.currentTimeMillis();
46 lazy();
47 int result=n1/n2;
48 System.out.println(n1+"/"+n2+"="+result);
49 Long span= System.currentTimeMillis()-start;
50 System.out.println("共用时:"+span);
51 return result;
52 }
53
54 //模拟延时
55 public void lazy()
56 {
57 try {
58 int n=(int)new Random().nextInt(500);
59 Thread.sleep(n);
60 } catch (InterruptedException e) {
61 e.printStackTrace();
62 }
63 }
64 }
缺点:
1、工作量特别大,如果项目中有多个类,多个方法,则要修改多次。
2、违背了设计原则:开闭原则(OCP),对扩展开放,对修改关闭,而为了增加功能把
每个方法都修改了,也不便于维护。
3、违背了设计原则:单一职责(SRP),每个方法除了要完成自己本身的功能,还要计算
耗时、延时;每一个方法引起它变化的原因就有多种。
4、违背了设计原则:依赖倒转(DIP),抽象不应该依赖细节,两者都应该依赖抽象。而
在 Test 类中,Test 与 Math 都是细节。
剩余38页未读,继续阅读
资源评论
千源万码
- 粉丝: 764
- 资源: 368
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 王姿.html
- 51单片机学习(1)-软件keil下载
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功