从面相对象编程到面相切面编程,是一种代码组织方式的进化。

每一代的代码组织方式,其实是为了解决当时面对的问题。比如写编译器和写操作系统的时候的年代当然要pop,比如写界面的时候当然要oop,因为界面这东西是人造的约定俗成的,继承关系清晰明了,当写互联网软件的时候自然要面相服务sop。大型商务系统的编写,问题就更加复杂,aop就应运而生了。

AOP 应用:
1. 监控系统重要API的调用事件,用来监控系统的性能。
2.Authentication 权限
3. Caching 缓存
4. Context passing 内容传递
5. Error handling 错误处理
6. Lazy loading 懒加载
7. Debugging  调试
8. logging, tracing, profiling and monitoring 记录跟踪 优化 校准
9. Performance optimization 性能优化
10. Persistence  持久化
11. Resource pooling 资源池
12. Synchronization 同步
13. Transactions 事务

我们用最简单的性能监控来举个例子,记录一个服务所消耗的时间.

服务类,计算一个数的阶乘:

public class FactorialUtil {
public static int exFactirial(int n){
PerformanceMonitor.begin("FactorialUtil -- exFactirial");
int result = 1;
for(int i=n;i>0;i--){
result = result * i;
}
PerformanceMonitor.end();
return result;
}
}

里面使用了PerformanceMonitor类,来计算时间,这个类特别的简单:

public class PerformanceMonitor {
private static ThreadLocal<Long> beginTime = new ThreadLocal<>(); public static void begin (String url){
beginTime.set(System.currentTimeMillis());
System.out.println("begin monitor "+url);
} public static void end (){
long endTime = System.currentTimeMillis();
System.out.println("end monitor");
long costTime = endTime - beginTime.get();
System.out.println("the method cost : " + costTime +"ms");
}
}

线程安全,记录begin的系统时间和在end的时候把消耗时间减出来。

这样写代码有个好处,代码清晰明了,问题是阶乘计算类需要插入性能监控相关的代码,如果性能监控的代码需要修改,修改起来也比较麻烦。如果什么功能也需要添加性能监控,加起来也是很蛋疼的事。这种代码枯燥无味,全是套路不说,还容易出错。

因此第一个版本的解决方案旋即提出,他使用的jdk提供的动态代理的功能。实现方式如下:

首先将功能实现分为接口层和实现层。以期将功能代码标准化,能面相接口编程

public abstract class FactorialService {
public abstract int exFactirial(int n);
}
public class FactorialServiceImpl extends FactorialService {
@Override
public int exFactirial(int n) {
int result = 1;
for(int i=n;i>0;i--){
result = result * i;
}
return result;
}
}

第二步,将性能监控的代码,封装在jdk动态代理提供的handler类之中:

public class PerformanceHandler implements InvocationHandler {
private Object target;
public PerformanceHandler(Object obj){
this.target = obj;
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
PerformanceMonitor.begin("FactorialUtil -- exFactirial");
Object result = method.invoke(target,args);
PerformanceMonitor.end();
return result;
}
}

第三步,调用服务,使用java的proxy类,将服务包裹起来。

    public static int invokeByproxy(int n){
FactorialService target = new FactorialServiceImpl();
PerformanceHandler handler = new PerformanceHandler(target);
FactorialService proxy = (FactorialService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
handler);
return proxy.exFactirial(8);
}

这样写,也就是用了个代理模式。细节上倒是没什么瑕疵,问题只有一个成本太高,类太多。出错不容易,但是估计也没有多少人愿意这么写。接着新一代技术就出来了Cglib技术,本质差不多,先上写法:

public class CglibProxy implements MethodInterceptor {
private Enhancer enhancer= new Enhancer(); public Object getProxy(Class clazz){
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return enhancer.create();
} public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
PerformanceMonitor.begin("FactorialUtil -- exFactirial");
Object result = methodProxy.invokeSuper(o, args);
PerformanceMonitor.end();
return result;
}
}

代理类实现和上面的代理类实现差别不大,使用了Cglib jar包中的MethodInterceptor接口。类的内部使用的是Enhancer来实例化。

调用方式特别简单:

    public static int invokeByCglib(int n){
CglibProxy proxy = new CglibProxy();
FactorialServiceImpl service = (FactorialServiceImpl)proxy.getProxy(FactorialServiceImpl.class);
return service.exFactirial(8);
}

好处:减少了实现接口类,只要实现功能类和代理类就可以了。

问题:1.目标类里面的所有方法都被增加性能监控了。

  2.只能已方法作为单位增加性能监控的位置。

  3.每个新功能都要增加代理类,写起来很麻烦。

Spring AOP (Spring 3.x 企业应用开发实战读书笔记第六章)的更多相关文章

  1. Spring 3.x 实践 第一个例子(Spring 3.x 企业应用开发实战读书笔记第二章)

    前言:工作之后一直在搞android,现在需要更多和后台的人员交涉,技术栈不一样,难免鸡同鸭讲,所以稍稍学习下. 这个例子取自于<Spring 3.x 企业应用开发实战>一书中的第二章,I ...

  2. 《精通Spring4.X企业应用开发实战》读后感第六章(容器事件)

  3. 《精通Spring4.X企业应用开发实战》读后感第六章(国际化)

  4. 《精通Spring4.X企业应用开发实战》读后感第六章(引用Bean的属性值)

  5. 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)

  6. 《精通Spring4.X企业应用开发实战》读后感第六章(属性编辑器)

  7. 《精通Spring4.X企业应用开发实战》读后感第六章(内部工作机制、BeanDefinition、InstantiationStrategy、BeanWrapper)

  8. iPhone与iPad开发实战读书笔记

    iPhone开发一些读书笔记 手机应用分类1.教育工具2.生活工具3.社交应用4.定位工具5.游戏6.报纸和杂志的阅读器7.移动办公应用8.财经工具9.手机购物应用10.风景区相关应用11.旅游相关的 ...

  9. 《精通Spring4.X企业应用开发实战》读后感第七章(AOP基础知识、jdk动态代理,CGLib动态代理)

随机推荐

  1. asp.net 分析器错误消息: 文件.aspx.cs”不存在错误

    发布webapplication时后老是报告分析器错误消息: 文件.aspx.cs”不存在错误,差点抓狂,后来在网上搜到原因是: <%@ Page Language="C#" ...

  2. 【转】#include_next <filename.h>

    转载自 http://bbs.csdn.net/topics/390381450 #include_next仅用于特殊的场合. 它被用于头文件中(#include既可用于头文件中, 又可用于.c文件中 ...

  3. Delphi的移动文件方法(转)/删除文件:/文件的复制

    RenameFile,DeleteFile,MoveFile Delphi的移动文件方法 uses  ShellApi; procedure ShellFileOperation(fromFile: ...

  4. (4)ARP:地址解析协议

    一.简介 ARP为IP地址到对应的硬件地址之间提供动态映射.之所以称为动态是因为这个过程是自动完成的.一般应用程序用户或系统管理员不必关心.RARP是被那些没有磁盘驱动器的系统使用,它需要系统管理员进 ...

  5. mysql之一

    MySQL or MariaDB 简介 DBMS:数据库管理系统 RDBMS:关系型数据库管理系统    总之:他们都是一个数据管理程序:大多都是CS架构,都有专门的通信协议进行数据交换 关系模型: ...

  6. Oracle- 日期格式和数字类型处理

    更新数据库时间格式的显示格式的语句:(alter session set nls_date_format='YYYY-MM-dd'); to_date("要转换的字符串",&quo ...

  7. 配置MySQL主从双向同步

    原文地址:http://www.cnblogs.com/zhongshengzhen/ 原主数据库:192.168.137.33 原从数据库:192.168.137.197   需要先阅读并操作:ht ...

  8. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践

    http://www.cnblogs.com/xishuai/p/ddd-repository-iunitofwork-and-idbcontext.html

  9. C#程序实现动态调用DLL的研究

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  10. ZOJ 3822 Domination 期望dp

    Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...