因为非模块化的横切关注点会导致代码的混乱和代码的分散,所以我们都希望有一种能将其模块化的方法。
在这里我们选择代理设计模式将横切关注点从核心关注点分离出去,代理设计模式的原理是使用一个代理将对象包装起来,然后用该代理对象取代原来的对象,任何对原来的对象调用都首先要通过代理,与此同时,围绕着每个方法的调用,代理对象也可以执行一些额外的人物,代理非常适合实现横切关注点。
还是上面计算器的例子,我们创建一个日志代理,通过实现InvocationHandler接口,可以编写一个记录方法的开始和结束调用的处理程序。
public class ComputeLoggingHandler implements InvocationHandler{
private Log log = LogFactory.getLog(this.getClass());
private Object target;
public ComputeLoggingHandler(Object target){
this.target = target;
}
public static Object createProxy(Object target){
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),new ComputeLoggingHandler(target));
}
public Object invoke(Object arg0, Method method, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
log.info("the method "+method.getName()+"() start");
System.out.println("the method "+method.getName()+"() start");
Object result = method.invoke(target, arg2);
log.info("the method "+method.getName()+"() end"+result);
System.out.println("the method "+method.getName()+"() end"+result);
return null;
}
}
public class Main {
public static void main(String args[]){
Compute computeImpl = new ComputeImpl();
Compute compute = (Compute)ComputeLoggingHandler.createProxy(computeImpl);
compute.add(1, 2);
}
}
我们通过ComputeLoggingHandler代理实现InvocatonHandler接口invoke()方法,它允许控制整个调用过程
Invoke方法第一个参数是代理实例,正在调用的是它的方法。
第二个参数是method方法对象,代表当前正被调用的方法。
第三个参数是北调用的目标方法的参数数组。
最后作为当前方法调用的结果,必须返回一个值。
下一篇:spring recipes笔记 - 使用经典的spring通知来模块化横切关注点
分享到:
- 2009-10-26 00:10
- 浏览 874
- 评论(0)
- 论坛回复 / 浏览 (0 / 1362)
- 查看更多
相关推荐
获取Spring Boot 2微框架的可重用代码配方和代码段 了解Spring Boot 2如何与其他Spring API,工具和框架集成 访问Spring MVC和新的Spring Web Sockets,以实现更简单的Web开发 使用微服务进行Web服务开发并与Spring ...
Spring Recipes A Problem-Solution Approach (Books for Professionals by Professionals).rar
关注最新Spring 2.5构建企业Java应用。 This book focuses on the latest Spring 2.5 features for building enterprise Java applications.
Spring Recipes: A Problem-Solution Approach, Second Edition With over 3 Million users/developers, Spring Framework is the leading “out of the box” Java framework. Spring addresses and offers simple...
Spring Recipes A Problem-Solution Approach
赠送jar包:curator-recipes-2.6.0.jar; 赠送原API文档:curator-recipes-2.6.0-javadoc.jar; 赠送源代码:curator-recipes-2.6.0-sources.jar; 赠送Maven依赖信息文件:curator-recipes-2.6.0.pom; 包含翻译后...
Spring Recipes A Problem-Solution Approach Third Edition 覆盖Spring 4.0
Spring 5 Recipes A Problem-Solution Approach(4th).pdfSpring 5 Recipes A Problem-Solution Approach(4th).pdf
Spring Recipes: A Problem-Solution Approach, Third Edition builds upon the best-selling success of the previous editions and focuses on the latest Spring Framework features for building enterprise ...
Using a problem-solution approach, Spring Boot 2 Recipes quickly introduces you to Pivotal's Spring Boot 2 micro-framework, then dives into code snippets on how to apply and integrate Spring Boot 2 ...
Spring Recipes, 3rd Edition.pdf
赠送jar包:curator-recipes-4.3.0.jar; 赠送原API文档:curator-recipes-4.3.0-javadoc.jar; 赠送源代码:curator-recipes-4.3.0-sources.jar; 赠送Maven依赖信息文件:curator-recipes-4.3.0.pom; 包含翻译后...
android recipes--A problem-solution approach 3rd
赠送jar包:curator-recipes-2.6.0.jar; 赠送原API文档:curator-recipes-2.6.0-javadoc.jar; 赠送源代码:curator-recipes-2.6.0-sources.jar; 赠送Maven依赖信息文件:curator-recipes-2.6.0.pom; 包含翻译后...
SPRING RECIPES-A PROBLEM SOLUTION APPROACH
One of the first (if not the first) books on the latest Spring 3.x, and the first Spring code Recipes book focused on Spring Web-tier development The Spring framework is growing. It has always been ...
nginx-cookbook-recipes-high-performance
Spring Recipes 3rd Edition Sources === These are the sources belonging to Spring Recipes 3rd Edition. Each chapter has its own sources and each chapter can contain multiple source snippets TOC --- ...
The recipes in Spring 5 Recipes cover Spring fundamentals such as Spring IoC container, Spring AOP/ AspectJ, and more. Other recipes include Spring enterprise solutions for topics such as Spring Java ...