Synchronization from: http://www.blogjava.net/anderslin/archive/2006/06/24/54806.html
In AOSD: Apply AOP to implement business logic, I put forward the interface; the original text on Javaeye is also proposed to use Event, or Intercepter. Of course, different ways can solve different problems. Here is the difference between AspectJ and Event, and Intercepter.
1. Let's take a look at Event: Two objects Event and EventHandler (EventListener), Event, and EventHandler are data contracts. In other words, the advantage of sacrificing the compilation check can, of course, there must be a certain advantage when changing. Once the demand changes, there is a lot of workloads on the code on the code, and more serious is that the invasiveness of the wolf is too strong. 2 Then look at the interceptor's AOP, relative to the EVENT approach, although it is based on a data contract, it has been transformed into yourself, but the amount of code is less (because the code generation) is, it is not an invasivity like Event. 3. Wolf square is improved, using AOP to do BrocaSteent's work. There is still a lot to do, and even I think this work is better to use Interceptor to be directly used.
For example, Domain Service, which is the domain model (Domain model) to handle two logic: business rules and process logic, and AOSD: Apply AOP to implement business logic to try to solve the problem is logic on the process, and above The way is unable to solve a problem: process information. Assume that the existing processes are as follows:
public
Class
DomainService
{PUBLIC VOID BIZPROCESSA () {SomeObject Instance = DOSMEAAAACTION ();} public void bizprocessb () {someObject instance = DOSMEAAAAAAAAAAAAAAAAAAACTION () PUBLIC SomeObject DosomeAction ()}}}
Here we pay attention to the goal is dosomeAction (), in DosomeAction, we want to do additional work, with the wolf square example, such as: FinancialService.createRequestofmoney (...); then whether it is Event:
public
SomeObject DosomeAction ()
{Brocaste (new evenet ());
Or Interceptor:
public
Class
SomeInterceptor
IMPLEMENTS
MethodInterceptor
{Publicinvocation invoke {Obj = invoke.proceed (); // Perform the intercepted method to complete the business operation. FinancialService.createRequestofmoney (.);}}
We noticed that the result of this is that the different business processes in bizprocessa () and bizprocessB () will result in FinancialService.createRequestOfmoney but our process logic requires: Under the bizprocessa () process, perform FinancialService.createRequestofmoney Under the bizprocessb process, FinancialService.createRequestofmoney is not executed. Very obvious, both Event, or the interceptor AOP can solve this problem.
In the face of such a need, it is necessary to improve or rebuild: 1. The simplest is also the most directly changed DOSMEAAAction (... Boolean Flag). It is obvious that this approach is very unsatisfactory. However, it is still necessary to solve dynamic loading problems, and processes the means of time.
Don't really have these two means? Do not! Also, that is, AspectJaspectj provides the following WITHIN WITHINCIDEWIDECDECFLOWCFLOWBELOW a few built-in Pointcut, which will be able to do what we want, the code is as follows:
public
Aspect serviceaspect
{PointCut BP (): cflow (Void ")); PointCut Action (): Call (DosomeAction ()); SomeObject Around (): BP () && Action () && args () { SomeObject instance = proceed (); FinancialService.createRequestofmoney (.); Return Instance;}}
Obviously, AspectJ offers the arms needed by AOSD.
BTW: This article is not for the wolf square classmates, just through its two blog discussions, explains the AspectJ advantages, and why Aspectj is the best military fire in achosc. In fact, there are many ways to solve the problem, this article is intended to clarify ASD to provide us with another idea. Welcome to discuss and shoot bricks!
Also replenish my complete example of the complete example of Javaeye discussion, download.