`
Tyrion
  • 浏览: 260955 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Execute Around Method

    博客分类:
  • Java
 
阅读更多

“前人栽树,后人乘凉”,搞Java的,不知道模式,那这人绝对还未入流。但经常是写了这么多代码,看看才发现,原来前人早就总结过了,自己满头大汗的栽了棵树苗,蓦然回首,擦,远方已经有棵大树鸟。

 

就拿这个模式来说,名字挺吓人的,其实估计搞过Java(或者被搞)的大概都会用过,业务场景是,对于输入输出流,JDBC连接等资源,一般的处理方式是谁开启的就由谁负责关闭资源,其中必然涉及对于异常的处理,而如果这个资源不仅仅是当前对象要用,还可能传递给别的对象使用的话,一般的做法就是。。。不说废话了,贴代码吧: 

 public void useResource(ResourceUser user){
		Resource resource = null;
		try{
			user.use(resource.open());
		}
		catch (Exception e){
			user.onError(e);
		}
		finally{
			if (resource != null){
				try{
					resource.close();
				}
				catch (Exception e){
					user.onError(e);
				}
			}
		}
	}

 其它的资源使用者直接在use()方法里面做自己的相关业务就行了。

 

这种处理方式学名就叫Execute Around Method,其实不光资源处理是这种方式,稍微扩展一下会发现,什么AOP、动态代理的处理也是这种方式。

 

“一开始没有模式,起了个名字就是模式”。。。

分享到:
评论

相关推荐

    Smalltalk Best Practice Patterns Kent Beck

    - **执行环绕方法(Execute Around Method)**:在执行特定操作前后添加额外的行为。 - **调试打印方法(Debug Printing Method)**:帮助开发者在调试过程中更好地理解对象的状态。 ##### 消息(Messages) - **消息...

    AOP编程示例

    通过在切面类上定义`@Aspect`,在方法上定义`@Before`, `@After`, `@Around`等注解,可以轻松地定义切点和通知。以下是一个Spring注解AOP的例子: ```java import org.aspectj.lang.annotation.Aspect; import org....

    castle-AOP

    <around method="Execute*" advice="MyNamespace.MyAdvice, MyAssembly"/> ``` - **代码注解**:使用特性(Attribute)在类或方法上标记切面应用。 ```csharp [ interceptions ] public class MyComponent...

    spring boot如何使用spring AOP实现拦截器

    logger.info("请求结束,方法:{},执行时间:{}ms", methodName, executeTime); } catch (Throwable e) { // 记录异常信息 logger.error("请求异常,方法:{},异常:{}", methodName, e.getMessage()); result = ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    Instead, LLDB will generate a synthetic name for a method it recognizes as a method, but doesn’t know what to call it. In this chapter, you’ll build an LLDB script that will resymbolicate stripped ...

    spring基于xml文件的aop操作,

    <aop:around method="aroundAdvice" pointcut-ref="executionPointcut"/> ``` ```java public class MyAspect { public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { System.out....

    学习Spring笔记_AOP_Annotation实现和XML实现

    System.out.println("Method " + joinPoint.getSignature() + " is about to execute."); } } ``` 然后,我们来看看XML配置实现AOP: 1. **XML配置实现AOP** 在Spring的配置文件中,我们可以创建`<aop:config>`...

    spring aop两种配置方式

    } // 后置通知,在方法正常执行后调用 @After("pointCut()") public void after() { System.out.println("after method execute..."); } // 异常通知,在方法抛出异常后调用 @AfterThrowing(pointcut = "pointCut()...

    INTRODUCTION TO PYTHON SCRIPTING FOR MAYA ARTISTS.

    Object-oriented programming (OOP) is a paradigm that organizes code around objects and data, rather than actions and logic. - **Classes:** Classes define blueprints for objects. They specify the ...

    Senfore_DragDrop_v4.1

    This is believed to be a bug in the Windows clipboard and a work around hasn't been found yet. * Asynchronous targets appears to be broken in the current release. * When TDropFileTarget....

    基于方法的切片缓存插件Aspect-Cache-Plug.zip

     <aop:around method="execute" pointcut-ref="adviceAspectPoint" arg-names="Aspect" />        id="adviceAspect" class="com.aspect.AdviceAspect">   name="rootKey" value="aspect.cache."/> ...

    acpi控制笔记本风扇转速

    (execute any AML debugger command) (Valery Podrezov). ---------------------------------------- 12 September 2006. Summary of changes for version 20060912: 1) ACPI CA Core Subsystem: Enhanced the ...

    Shaspect:C#的AOP(面向方面​​编程)的实现

    - **通知(Advice)**:通知是在特定连接点执行的行为,比如前通知(Before)、后通知(After)和环绕通知(Around)。 - **连接点(Join Point)**:连接点是在程序执行过程中可以插入通知的特定点,通常是方法...

    Intermediate Perl.pdf

    Work with classes, method calls, inheritance, and overriding. Chapter 12, Objects with Data Add per-instance data, including constructors, getters, and setters. Chapter 13, Object Destruction Add ...

    groovy_in_action_draft_ch_01.pdf

    - **Closures**: Groovy supports closures, which are anonymous functions that can be passed around as values. Closures are useful for implementing callbacks, event handlers, and functional programming ...

    C# - CSharp 12 in a Nutshell The Definitive Reference

    C# is an object-oriented programming (OOP) language, which means that it structures code around objects rather than actions and data instead of logic. Object orientation provides several key benefits,...

    jsp编程技术复习题

    - **Java反射机制**:Spring框架的IoC容器使用了大量的Java反射机制,这包括`Class`、`Field`、`Method`和`Constructor`等类,以实现在运行时创建新类、调用方法等功能。 - **EL表达式与OGNL的区别**:EL表达式可以...

    google api php client

    Once composer is installed, execute the following command in your project root to install this library: ```sh composer require google/apiclient:"^2.0" ``` Finally, be sure to include the autoloader:...

    javacv-platform-1.3.3-src

    JavaCV also comes with hardware accelerated full-screen image display (CanvasFrame and GLCanvasFrame), easy-to-use methods to execute code in parallel on multiple cores (Parallel), user-friendly ...

    VB编程资源大全(英文源码 数据库)

    It is handy in studying database programming.<END><br>20 , MySQLExplorer.zip Explorer for MySQL Data Base.You can retrieve and execute your query. <END><br>21 , ICDBaseSource115.zip DAO based ...

Global site tag (gtag.js) - Google Analytics