- Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure.
- The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.
- Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects.
- Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects.Put it simple, it’s just an interceptor to intercept some processes, for example, when a method is execute, Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.
For example: Logging, Transaction and security are some Aspects.
In Logging we may have different aspect.
- AOP is a pattern used to modularize cross cutting features. So if there is a certain “thing” that applies to a significant part in your code then you can use AOP to solve that problem.
- Aspect Oriented Programming is sensibly new and it is not a replacement for Object Oriented Programming.
In fact, AOP is another way of organizing your Program Structure. Basically AOP increased you modularity of application by separation of cross-cutting concerns. such as it helped you lot at the time of transaction management logging etc.
AOP in spring is used
- To provide declarative enterprise services such as declarative transaction management
- To allow users to implement custom aspects.
- Spring AOP used to tracking entire activity of user in large business applications.
- More like logging using AOP we can identify user consumed classes and methods and
if any error occurs in any method, developers or customer helpers can identify problem
soon and can give solution soon than reading log files.
Spring AOP Concept
A modularization of a concern that cuts across multiple classes.Transaction management is a good example of a crosscutting concern in enterprise Java applications.
A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.
This is an object which includes API invocations to the system wide concerns representing the action to perform at a joinpoint specified by a point.
A pointcut defines at what joinpoints, the associated Advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework. Of course, you don’t want to apply all of your aspects at all of the possible joinpoints. Pointcuts allow you to specify where you want your advice to be applied. Often you specify these pointcuts using explicit class and method names or through regular expressions that define matching class and method name patterns. Some AOP frameworks allow you to create dynamic pointcuts that determine whether to apply advice based on run time decisions, such as the value of method parameters.
object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
Types of advice:
1. Before advice:
Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
2. After returning advice:
Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
3.After throwing advice:
Advice to be executed if a method exits by throwing an exception.
4. After (finally) advice:
Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
5. Around advice:
Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.