Spring AOP Interview Questions

After going through spring core interview questions, let’s cover the top Spring AOP interview questions we may expect in our next technical interview. Again, please feel free to suggest new questions that are not part of this post to include them to benefit a larger audience.

1. Describe Spring AOP?

Spring AOP (Aspect Oriented Programming) compliments OOPs in the sense that it also provides modularity. In OOPs, the key unit is Objects, but in AOP, the key unit is aspects or concerns (simply assume stand-alone modules in your application).

Some aspects have centralized code, but others may be scattered or tangled, e.g. logging or transactions. These scattered aspects are called cross-cutting concerns. A cross-cutting concern is a concern that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc.

AOP provides the way to dynamically add the cross-cutting concern before, after or around the actual logic using simple pluggable configurations. It makes it easy to maintain code in the present and future as well. You can add/remove concerns without recompiling the complete source code simply by changing configuration files (if you are applying aspects using XML configuration).

We can use spring AOP in 2 ways given below. But the widely used approach is Spring AspectJ Annotation Style.

2. Difference between Concern and Cross-cutting Concern in Spring AOP?

A Concern is a behavior that we want to have in an application module. Concerns may be defined as functionality we want to implement to solve a specific business problem. For example in any eCommerce application, different concerns (or modules) may be inventory management, shipping management, user management etc.

A cross-cutting concern is a concern that is applicable throughout the application (or more than one module). e.g. logging, security and data transfer are the concerns needed in almost every module of an application, hence they are termed as cross-cutting concerns.

3. What are the Available AOP Implementations in Java?

Main Java-based AOP implementations are listed below :

  1. AspectJ
  2. Spring AOP
  3. JBoss AOP

We can find the big list of AOP implementations on the wiki page.

4. What are the Different Types of Advices?

An advice is the implementation of cross-cutting concern that we are interested in applying to other modules of the application. Advices are of mainly five types :

  1. Before advice : Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception). To use this advice, use @Before annotation.
  2. After returning advice : Advice to be executed after a join point completes normally. For example, if a method returns without throwing an exception. To use this advice, use @AfterReturning annotation.
  3. After throwing advice : Advice to be executed if a method exits by throwing an exception. To use this advice, use @AfterThrowing annotation.
  4. After advice : Advice to be executed regardless of the means by which a join point exits (normal or exceptional return). To use this advice, use @After annotation.
  5. Around advice : Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. To use this advice, use @Around annotation.

5. What is Spring AOP Proxy?

A proxy is a well-used design pattern. To put it simply, a proxy is an object that looks like another object, but adds special functionality behind the scene.

Spring AOP is proxy-based. AOP proxy is an object created by the AOP framework to implement the aspect contracts in runtime.

Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied. Spring AOP can also use CGLIB proxies. This is necessary to proxy classes, rather than interfaces.

CGLIB is used by default if a business object does not implement an interface.

6. What are Introductions in Spring AOP?

Introductions enable an aspect to declare that advised objects implement an additional interface(s) which they don’t have in real, and to provide an implementation of that interface on behalf of those objects.

An introduction is made using the @DeclareParents annotation.

Read more about introductions.

7. What are Join points and Point cuts?

A Join-point is a specific point in the program’s execution, such as the execution of a method or the handling of an exception. In Spring AOP, a join-point always represents a method execution.

For example, all the methods defined inside your EmployeeManager interface can be considered joint points if you apply any cross-cutting concern on them.

A pointcut is a predicate or expression that matches join-points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, expression “execution(* EmployeeManager.getEmployeeById(..))” to match getEmployeeById() the method in EmployeeManager interface).

The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

8. What is Weaving?

The Spring AOP framework supports only limited types of AspectJ pointcuts and allows aspects to apply to beans declared in the IoC container. Suppose you want to use additional pointcut types or apply your aspects “to objects created outside the Spring IoC container“. In that case, you have to use the AspectJ framework in your Spring application and use its weaving feature.

Weaving is the process of linking aspects with other outsider application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime.

Like other pure Java AOP frameworks, Spring AOP performs weaving at runtime. In contrast, the AspectJ framework supports both compile-time and load-time weaving.

AspectJ compile-time weaving is done through a special AspectJ compiler called ajc. It can weave aspects into your Java source files and output woven binary class files. It can also weave aspects into your compiled class files or JAR files. This process is known as post-compile-time weaving. You can perform compile-time and post-compile-time weaving for your classes before declaring them in the Spring IoC container.

Spring is not involved in the weaving process at all. Please refer to the AspectJ documentation for more information on compile-time and post-compile-time weaving.

AspectJ load-time weaving (also known as LTW) happens when the target classes are loaded into JVM by a class loader. For a class to be woven, a special class loader is required to enhance the bytecode of the target class. Both AspectJ and Spring provide load-time weavers to add load-time weaving capability to the class loader. You need only simple configurations to enable these load-time weavers.

Now it’s your turn to share more Spring AOP interview questions that you have faced in previous interviews so that I can include them in this post and make it more useful for others as well.

9. How to Implement AOP in Spring Boot?

To implement AOP, start with including the spring-boot-starter-aop module in the application dependencies. It transitively imports spring-aop and aspectjweaver dependencies into the application.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Next, we enable the autoconfiguration using the @EnableAspectJAutoProxy annotation.

@Configuration
@EnableAspectJAutoProxy
public class AopConfig {
 
}

Now we are ready to create various AOP aspects using the @Aspect annotation. Read a detailed tutorial on this Spring Boot AOP example.

@Aspect
@Component
public class LoggingAspect 
{
    @Around("execution(* com.howtodoinjava.aop..*(..)))")
    public Object profileAllMethods(
        ProceedingJoinPoint proceedingJoinPoint) throws Throwable 
    {
        MethodSignature methodSignature = 
            (MethodSignature) proceedingJoinPoint.getSignature();
        //...
    }
}

10. Can We Define Ordering of the Multiple Aspects?

Yes, we can define the ordering of the aspects using the annotation @Order.

@Aspect
@Order(0)
public class LoggingAspect
{
    //
}

@Aspect
@Order(1)
public class TransactionAspect
{
    //
}

Another technique is to implement the org.springframework.core.Ordered interface in the Aspect class.

@Aspect
public class LoggingAspect implements Ordered
{
    @Override
    public int getOrder() {
        return 0;
    }
    //...
}

@Aspect
public class TransactionAspect implements Ordered
{
    @Override
    public int getOrder() {
        return 1;
    }
    //...
}

Happy Learning !!

Leave a Reply

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial