QA InfoTech » DevLabs Expert Group » Aspect Oriented Programming in Java Spring
Aspect Oriented Programming in Java Spring

Aspect Oriented Programming in Java Spring

Aspect Oriented Programming in Java Spring

Aspect Oriented Programming abbreviated as AOP compliments OOPs concepts as it provides modularity. And the key unit of modularity is considered here as an aspect rather than a class. AOP breakdowns the program logic into various distinct parts (i.e. concerns). It is generally used for increasing the program modularity by cross-cutting concerns.

A cross-cutting concern or middleware service is the one that could affect your application and should be centralized under the same block of the code like transaction management, security,authentication, logging, mailing and so on. By using Spring AOP, one can write the actual application and other middleware services code independently in a loosely coupled manner, moreover, you can link these codes by any of the AOP’s implementation approach either XML or annotations. So, the main aim of AOP could be defined as to separate the various services from the actual businessapplication code. By implementing this, it will make easy to maintain the code in the future as well.


The key factor is that AOP provides a pluggable way for adding the concern before, after or around the actual logic in a dynamic manner. Let us suppose there is ‘n’ number of methods out of which in ‘m’ number of methods, we have to implement logs before and after calling those methods. Without AOP, the problem is we have to write the log code in all ‘m’ methods, but in case if the logging is not required in all or some of the methods in the future, we need to again change those methods that will raise a problem of maintenance. Now, AOP can solve this problem by defining the concern named as maintaining a log of the method call. Its entry could be given in the XML file or as an annotation. So if in future, we have to remove the logging functionality, we need to change the XML file only which means maintenance is easy in AOP.


Spring AOP consists of the various concepts and terminologies, as shown in the below diagram :

Terminologies and Concepts of AOP


Aspect: It is defined as the service class that contains advice, join points, etc. For example, a transaction module would be known as AOP aspect for the transaction. Aspects can be configured through XML or using the Annotation @Aspect.

Join-point: It is the point in your business logic method where the advice can be applied. This point can be the execution of a method or handler of an exception being thrown.

Advice: This could be defined as the actual code or action which is performed for a join-point i.e. during the method execution either before or after or both.

Pointcut: It is defined as a point or a condition expression for executing the aspects for actual business logic methods by linkage of the advice with the join-points.

Target ObjectIt is the object on which advices/services will be applied. It always is a proxied/cached object.

ProxyIf you add business along with your service, it will combine these both and finally delivers a proxy object after applying the advice on the target object which will a child object of that business class.

Weaving: In simple terms, weaving means combining or linking, which links both your advice and application objects at runtime in Spring AOP.

AdvisorIt is defined as the combination of the point-cut and the advice.

Types of Advices :

There are different types of advices present in AOP defined below:

Before: In this advice get executed before the execution of the actual method, could be configured by @Before annotation.

After returning: Here, the advice gets executed only after the method execution completed successfully.

After throwingIt gets executed only the actual method executed by throwing an exception.

After (finally): It will be executed after the method execution irrespective of the method exit, whether it is terminated normally or with an exception.

AroundIt gets executed both before and after a join-point (i.e advised method execution) and can be configured by @Around annotation.

About the Author