25

After reviewing the AOP pattern, I'm overwhelmed with the ways of how and what to use it for in my spring project.

I'd like to use it as audit log system of all the financial business logic. It just seems to be easy to integrate. But I'd like to hear your take on this.

The question is - what other uses should I consider that are common for this pattern? I would not mind refactoring my current logic to be used with AOP as long as there is benefits to it.

laurent
  • 88,262
  • 77
  • 290
  • 428
MatBanik
  • 26,356
  • 39
  • 116
  • 178
  • 2
    There is a, a bit similar question: "What is Aspect J good for". http://stackoverflow.com/questions/4313789/what-is-aspectj-good-for – Ralph Jan 17 '11 at 10:09

7 Answers7

24

The most common usage is where your application has cross cutting concerns i.e. a piece of logic or code that is going to be written in multiple classes/layers.

And this could vary based on your needs. Some very common examples of these could be:

  1. Transaction Management
  2. Logging
  3. Exception Handling (especially when you may want to have detailed traces or have some plan of recovering from exceptions)
  4. Security aspects
  5. Instrumentation

Hope that helps.

Nilesh
  • 4,137
  • 6
  • 39
  • 53
14

Besides logging/auditing and declarative transaction handling as mentioned by Axel, I would say another usage of AOP is as a request interceptor. For example, let's say you need all requests coming of a server to be intercepted so that you can do something with it (may be to keep track of which app is sending what request to what other app or what database, etc).

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
10

The most common use is probably the declarative transaction handling using @Transactional.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Axel Fontaine
  • 34,542
  • 16
  • 106
  • 137
2

Using AOP for audit logging is a perfectly valid use of AOP. You can turn it off for testing and change it as requirements change in production.

The only downside in this case is if you were planning on doing the audit log via SQL. It may be more performant to implement this kind of auditing as triggers directly in the DB.

AngerClown
  • 6,149
  • 1
  • 25
  • 28
2

You can use AOP for your security concerns, for example allow/disallow method access. Another usage of aop is to test your application performance.

RicoZ
  • 835
  • 5
  • 16
2

It can be used to expose custom metrics (Instrumentation of service) for Alerting and Monitoring of service using client libraries like dropwizard, prometheus.

It helped us, to

  1. Keep this instrumentation code (Not a business logic) outside of actual business logic
  2. Keep these cross-cutting concerns at one single place.

  3. Declaratively apply them wherever required.

For example, To expose

  1. Total bytes returned by REST AIP - (Can be done in after advice)
  2. Total time taken by REST API i.e server-in and server-out rime- (Can be done using around advice).
Amit Patil
  • 710
  • 9
  • 14
1

As an answer slightly different from what @Axel said, using it to automatically intercept all of your data access calls and apply transactions appropriately is phenomenal. I have mine set up to implement all calls to my dao package that don't start with "get" in a transaction and then anything performed in a method starting with "get" is treated as read only. It's fantastic because aside from the initial setup, I don't have to worry about it, just follow the naming convention.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109