Open In App

Spring Boot – Difference Between AOP and OOP

Last Updated : 28 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

AOP(Aspect-Oriented Programming) complements OOP by enabling modularity of cross-cutting concerns. The Key unit of Modularity(breaking of code into different modules) in Aspect-Oriented Programming is Aspect. one of the major advantages of AOP is that it allows developers to concentrate on business logic. It is more convenient to use because changes need to be done in only one place. AOP is used along with spring Ioc to provide a very capable middleware solution.

Note: Cross cutting concerns are one of the concerns in any application such as logging, security, caching, etc. They are present in one part of the program but they may affect other parts of the program too.

AOP is used along with Oop as it also works around classes and objects, etc. We can also say that Oop is a basic term for AOP. Different Frameworks used in Aop are AspectJ, JBoss, and Spring. AOP makes the program loosely coupled. AOP separates business logic from cross-cutting concerns. The aspect class which contains cross-cutting concerns is annotated by @Aspect and @EnableAspectJAutoProxy annotations

AOP has different terms like Aspect, Weaving, different types of advices, JoinPoints and Pointcut expressions, etc. These terms are explained below:

  • Aspect: The cross-cutting concerns are modularized as Aspect. The classes which contain such cross-cutting concerns are annotated with @Aspect annotation.
  • Join point: Method execution is represented by using Joinpoint.
  • Advice: Aspect takes action on a particular Joinpoint. This action depends on various advice which is explained below:
  • Before advice: It runs before the method execution.
  • After returning advice: It runs after the result is returned by the method.
  • After throwing advice: It runs after an exception is thrown by the method.
  • After (finally) advice: It is executed after method execution or after an exception is thrown or the result is returned by the method.
  • Around advice: It can perform the behavior before and after the method invocation.
  • Pointcut: Pointcut is a signature that matches the join points.

Illustration: A pointcut expression with before advice:

// Annotation
@Before("execution(* abc.efg.gettingstarted.dao.*.add(..))")

public void allMethods(Point Point) 
{  // Aspect body }

Object-Oriented Programming

The object-oriented programming model works around classes and objects. The main building blocks of Oop are classes, objects, methods, attributes, etc. Oop has various advantages such as code reusability, flexibility, etc. It also maintains modularity using classes.

Note: Object is an instance of class and class is a blueprint of an object created.

The Key unit of Modularity(breaking of code into different modules) in Object-Oriented Programming is class. Oop contains objects, classes, interfaces, etc. Oop lacks the feature of using cross-cutting concerns. It consists of various concepts such as Data abstraction, Encapsulation, Polymorphism, and Inheritance.

Illustration: If there is a fruit class then apple, orange, banana are various objects of the fruit class.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads