读书人

AOP(一):应用中的几个小故事

发布时间: 2012-12-22 12:05:06 作者: rapoo

AOP(1):应用中的几个小故事

I had heared about AOP almost 7 years but only used it in two projects. 7 Years, I still only know few about AOP, if i were not involved in that two projects, i would still know that few.

The fact is always like that:

?

    Hear about it.Know how to use it.Use it in projectKnow what's problem it solved and try to enhance it.
First story: Why there is no "before" or "after" method execution Advice?

Normally for a AOP, we can say there are point-cuts or events:

?

    Befroe executionBefore returnException catch and processing in case exception raised

First time, I use AOP with EJB3. Thanks to annotation @Interceptor and @AroudInvoke, i can define AOP interceptors easily. But for a long time during, I was always wondering:

?

Why there is only "around?" invokea method, but no "before" or "after" invoke methods?In fact, AOP engine already get the hand from execution environment and give you the hand to execute the methods, so you can do whatever you want: before the method execution, after methods execution or catch and process the exception. Please remember to return the execution result in order to launch the successive interceptor chain or return the control to execution environment.
What's more, in fact there are dedicated before, throws and after returning advice(interceptor) in spring AOP. ?Second story: Exception used as logical condition had been intercepted by prior interceptorThere is one advisor to manage transaction, its main usage as following:
    Catch Exception from point-cut and retry execution in case there is exception.If max retry number catched, marks Transaction to be "Rollback Only".
Here is the snapshot for tx-advice?defintion?:

So I have to redesign the business logical:

    Do not set point-cut on class A.Or do not use exception as business logical condition.Or use some other adapter who provide same behavior but has no that transaction interceptor. It is like first workaround.
Third Story: It is really difficulty to debug impossible NullPointerException raised because of not-well defined interceptor.?It is like second story, but it makes situation more complex:There are some Interceptors/Advisors which are not defined by myself.So if other people define new interceptors for the classes I am working on, it will be difficulty to identify the error in integration test stage.

读书人网 >编程

热点推荐