What is AOP, and how does it relate to OOP?

AOP stands for Aspect Oriented Programming. It divides the application logic into parts called concerns. AOP is similar to OOP in terms of modularity. It differs from OOP in the way modularity is defined. In OOP, modularity is achieved by using classes whereas in AOP modularity is achieved using aspects that are applied to different class methods. Since aspects or concerns apply to the whole application, their processing should be centralized.

What is a cross-cutting concern in AOP?

A cross cutting concern is something that affects the whole application. Instead of handling it separately in every layer, it is handled in a centralized manner. Examples of cross cutting concerns are security, logging, transaction management, and error handling etc.

Is there a difference between the terms concern and cross cutting concern in Spring AOP?

Concern can be defined as the functionality of a particular module in the application. For example in an e-commerce application, concerns may be inventory management and user management etc.

A cross cutting concern is a concern that is applicable across multiple application layers. For example logging and security functionality is needed by every module of an application.

What problems does AOP solve?

AOP modularizes the code in terms of cross cutting concerns. The absence of AOP leads to two main problems.

  • Code tangling: the code for the cross cutting concern gets mixed with business logic.
  • Code scattering: Identical code is present in all modules.

Name some implementations of AOP?

Popular implementations of AOP include Apache AspectJ, JBoss by Red Hat and Spring AOP.

What is the difference between Spring AOP and AspectJ AOP?

Spring AOP does weaving at runtime using proxy while AspectJ does compile time weaving using AspectJ Java tools.

Only method level pointcuts are supported by Spring AOP while AspectJ also supports field level pointcut.

How does Spring implement a cross cutting concern?

Spring AOP separates the business logic and cross cutting concern. The developer focuses on the program logic. Spring provides many aspects for cross cutting concerns which can be woven into the application.

What is a proxy in Spring AOP?

In simple words, a proxy is an object that looks like another object but has some added functionality. In Spring AOP, proxy is the object created after applying advice to a target object.

Note: Proxy = Advice + Target Object

Target object is also called a proxy object.

What is a target object?

Target object is an object to which a cross cutting concern has been added. It is also called advised object or proxy object.

What advantage does Spring AOP provide?

Spring AOP allows us to add or change a concern without having to change the application code. Since we separate the concerns from the application logic, concerns can be dynamically plugged in before, after or around the application logic. The code also becomes easy to maintain. Another advantage is that the developer can concentrate on business logic rather than the cross cutting concerns.

Spring AOP configures aspects as normal beans. Also, no special compilation unit is needed when using Spring AOP.

What is a JoinPoint?

JoinPoint is a point in the program such as method execution where an aspect can be plugged in. There are different types of JoinPoints like field access, error handling and method execution.

Spring AOP only provides support for method execution join points. Any method inside the class can be called a join point if any cross cutting concern is applied to it and an aspect’s code is inserted into the normal flow of the application.

What is advice?

The logic of the aspect is called advice. It is the action that is taken when an aspect is executed. In terms of programming, advice is the execution of the method where a joinpoint matches a pointcut.

List the different types of advice in AOP?

There are 5 types of advice:

  • Before
  • After
  • After Returning
  • After Throwing
  • Around

Which advice type is appropriate for a try catch block?

In order to try and catch exceptions, the @AfterThrowing advice type is used. The method annotated with this annotation is run after the method exits by throwing an exception.

What is the difference between Joinpoint and ProceedingJoinPoint?

Proceedingjoinpoint extends the Joinpoint interface.

Joinpoint is used with the @Before, @After, @AfterReturning and @AfterThrowing advice types.

Proceedingjoinpoint is used with @Around advice. The @Around advice type is different from the rest because it can control when/if a method is executed. It also has a return value.

What is pointcut?

Pointcut is the expression that is matched to a JoinPoint to determine whether the advice should be executed or not. Spring framework uses the AspectJ pointcut expression language. These contain matching method or class name patterns.

What is a named pointcut?

When we need a pointcut at multiple places in the application, rather than using the lengthy pointcut expression, we can give it a name. This is done by creating a pointcut configuration class where we associate every pointcut with a method. Now the method name can be used in place of the long pointcut expression.

What is an Aspect?

An aspect is a class denoted by the @Aspect annotation. It contains advice and joinpoints. Aspect defines a concern that cuts across multiple application layers.

What is weaving?

Weaving is a process in which the aspects are plugged in at different points in the program execution. In Spring AOP, weaving is done at runtime. AspectJ provides both compile-time and load-time weaving.

When does the Spring framework perform weaving?

Spring framework performs weaving at runtime. The process of weaving aspects into the application classes takes place when the classes are being loaded in JVM.

Which AspectJ Pointcut Designators are supported by Spring AOP?

Spring provides support for some of the AspectJ Pointcut Designators (PCD) that can be used in pointcut expressions. For example:

  • execution: matches method execution joinpoints
  • within: matches to joinpoints of certain types
  • this: matches to joinpoints where the target object is of a given type
  • args: matches to joinpoints where the arguments of the given type
  • @annotation: matches to joinpoints where the method has a given annotation

Are there any limitations of Spring AOP?

  • Spring AOP only supports method-level joinpoints.
  • Advice is only applicable on public methods. Methods with private or protected visibility cannot be advised.
  • When weaving with proxies, advice is not executed on local method calls.
  • Aspects can only be applied to Spring beans.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.