Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most common use for AOP in spring project

Tags:

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.

like image 332
MatBanik Avatar asked Jan 16 '11 23:01

MatBanik


People also ask

What is AOP in spring used for?

AOP is used in the Spring Framework to... ... provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management.

Where is AOP used?

Some of the cases where AOP is frequently used: To provide declarative enterprise services. For example, as declarative transaction management. It allows users for implementing custom aspects.

Why do we use AOP?

You can use AOP to reduce code clutter by improving the readability and maintainability of your code. It should be noted that AOP is just a new programming paradigm -- it doesn't replace OOP in any way. Rather, it complements OOP by providing you another way to achieve modularity and also reduce code clutter.

Which of the following is the great useful use cases for spring AOP?

logging-related. security checks. transaction management. tweaking of a legacy application.


2 Answers

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.

like image 164
Nilesh Avatar answered Dec 26 '22 12:12

Nilesh


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

like image 31
CoolBeans Avatar answered Dec 26 '22 13:12

CoolBeans