Open In App

Spring Boot – Difference Between AOP and AspectJ

Improve
Improve
Like Article
Like
Save
Share
Report

Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Following are some of the features of Spring Boot:

  • It allows avoiding heavy configuration of XML which is present in spring
  • It provides easy maintenance and creation of REST endpoints
  • It includes embedded Tomcat-server
  • Deployment is very easy, war and jar files can be easily deployed in the tomcat server

AOP

Let’s understand some terminologies:

  • Aspect: The standalone distinct modules are called aspects. For example the logging aspect.
  • Join Points: This is a point where aspects can be injected to perform a specific task.
  • Advice: It is the actual code block, which will be executed before or after the method execution.
  • Pointcut: It is a set of one or more join points where the actual code will get executed. Pointcuts are specified using expression or patterns.

See the following example: 

Java




@Aspect
public class LoggingAspectPointCutExample{
  @PointCut("execution(*java11.fundamentals.*.*(...))")
  private void logData() {}
}


In the above example, we have used a few annotations. Let us understand their meanings.

  1. @Aspect: This annotation specifies that the class contains advice methods
  2. @PointCut: This annotation specifies the JoinPoint where advice should be executed.
  3. execution (*java11.fundamentals.*.*(..)): This specifics on which methods the given advice should be applied 
  4. Introduction: This enables the inclusion of new methods, fields, or attributes to the existing classes. It is also known as inter-type declarations in AspectJ. In other words, with an introduction, an aspect can assert advised objects to implement a specific interface. It can also provide an implementation of this interface.
  5. Target object: It specifies the object that is being advised by one or more aspects. This object will always be a proxied object as Spring AOP is implemented using runtime proxies. 
  6. AOP proxy: It denotes the object to implement the aspect contracts.
  7. Weaving: It is a process to create an advised object by linking one or more aspects with other objects or application types. There are various ways it can be done such as compile, load, or runtime. Spring AOP performs wearing at runtime.

AspectJ

AspectJ refers to a style of declaring aspects as regular Java classes annotated with annotations. Spring interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching. The AOP runtime is still pure Spring AOP though, and there is no dependency on the AspectJ compiler or weaver.

Difference Between AOP and AspectJ

Spring AOP

AspectJ AOP

It is not intended as a complete AOP solution – it can only be applied to beans that are managed by a Spring container. AspectJ is the original AOP technology that aims to provide a complete AOP solution.
It makes use of runtime weaving. It makes use of three different types of weaving: Compile-time weaving, Post-compile weaving, Load-time weaving.

It is a proxy-based AOP framework. This means that to implement aspects to the target objects, it’ll create proxies of that object. This is achieved using either of two ways:

JDK dynamic proxy, CGLIB proxy.

On the other hand, this doesn’t do anything at runtime as the classes are compiled directly with aspects.

And so unlike Spring AOP, it doesn’t require any design patterns.

It is much slower than AspectJ.  It has better performance.
It is easy to learn and apply. It is more complicated than Spring AOP.

Last Updated : 02 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads