Spring AOP Concepts and Terminology

By | June 25, 2021

In this post, We will learn about Spring AOP Concepts and Terminology.

One of the very important key units of Spring Framework is the Aspect-oriented programming (AOP) framework. Aspect-Oriented Programming helps us to provide functionalities to break down program logic into distinct parts or modules is called AOP concerns. The functions that spread across multiple sections of an application are called cross-cutting concerns and these cross-cutting concerns functionalities must be separate from the application’s business logic. There are various good examples of aspects like logging, authentication, transaction management, auditing, spring-security, caching, etc.

The key concept of modularity is Object-Oriented Programming is the class, whereas in AOP the unit of modularity is the aspect. Spring dependency Injection usually helps us to lose coupling our application objects from each other and AOP helps us to decouple cross-cutting concerns from the objects that they affect. 

Spring AOP provides interceptors to intercept an application. For example, when a method is executed, We can add extra functionality before or after the method execution.

AOP Terminologies

Before we start working with AOP, let’s become familiar with the AOP concepts and terminology. These terms are not specific to Spring Framework rather they are related to AOP(Aspect-oriented programming).

Sr.No Terms & Description
1 Aspect

This is a module that has a set of APIs which provides cross-cutting requirements. For example, a transaction management module would be called the AOP aspect for transaction management. An application may have any number of aspects depending on the requirement.

2 Joinpoint

The Joinpoint is a point in our application where We can plug in the AOP aspect. We can also say that it is the actual code or place in the application where an action can be taken using the Spring AOP framework.

3 Advice

The Advice is the actual action that has to be taken either before or after the method execution. This is an actual implementation or piece of code that is invoked during the program execution by the Spring AOP framework.

4 Pointcut

A pointcut is a set of one or many join points where advice can be executed. We can specify pointcuts using regular expressions or patterns.

5 Introduction

An introduction allows us to add new methods or attributes to the existing classes.

6 Target object

The object is being advised by one or more aspects. This object is always a proxy object, also It is referred to as the advised object.

7 Weaving

Weaving is the method to inject aspects with other application types or objects to create an advised object. This may be done at load time, compile-time or runtime.

Types of Advice

Spring AOP has five kinds of advice mentioned as Below −

Sr.No Advice & Description
1 before

This advice executes before a join point, but this advice does not have the ability to prevent execution flow to proceed to the join point (unless it throws an exception).

2 after

This advice is executed after a join point completes normally: for example if a method returns without throwing an exception.

3 after-returning

This advice runs after the method execution only if the method completes successfully.

4 after-throwing

This advice is executed if a method exiting by throwing an exception.

5 around

This is the most powerful kind of advice We can use around advice to perform custom behavior before and after the method invocation. This advice is also responsible to select whether to proceed to the joinpoint or to perform the advised method execution by returning its own return value or throwing an exception.

Custom Aspects Implementation

Spring Framework supports the @AspectJ annotation style approach and the XML schema-based approach to implement custom aspects. These two methods have been explained in detail in the following sections.

Sr.No Approach & Description
1 XML Schema-based Aspects are implemented using the regular classes along with XML-based Spring configuration.
2 The @AspectJ-based usually refers to a method to declare aspects as normal java classes annotated with AOP annotations.

That’s All about Spring AOP Concepts and Terminology

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.