MatBanik
MatBanik

Reputation: 26860

What is the most common use for AOP in spring project

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.

Upvotes: 26

Views: 22324

Answers (7)

Amit Patil
Amit Patil

Reputation: 790

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).

Upvotes: 2

Nilesh
Nilesh

Reputation: 4177

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.

Upvotes: 25

RicoZ
RicoZ

Reputation: 845

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

Upvotes: 2

Axel Fontaine
Axel Fontaine

Reputation: 35169

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

Upvotes: 10

Chris Thompson
Chris Thompson

Reputation: 35598

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.

Upvotes: 1

AngerClown
AngerClown

Reputation: 6229

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.

Upvotes: 2

CoolBeans
CoolBeans

Reputation: 20800

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).

Upvotes: 15

Related Questions