Spring AOP (Aspect-Oriented Programming) Interview Questions and Answers

1. What is the concept of AOP?

Aspect-oriented programming (AOP) is an approach to programming that sanctions the global properties of a program to determine how it is compiled into an executable program. AOP can be utilized with object-oriented programming (OOP). An aspect is a subprogram that is associated with a specific property of a program.

2. Why do we use AOP?

  • Reduced code clutter
  • Truncated code redundancy
  • More facile code maintenance
  • More expeditious development
  • Amended code readability

3. What is Spring AOP used for?

Spring AOP enables Aspect-Oriented Programming in spring applications. In AOP, aspects enable the modularization of concerns such as transaction management, logging, or security that cut across multiple types and objects.

4. What is advice, joinpoint, and pointcut?

JoinPoint: Joinpoint are points in our program execution where the flow of execution changed like Exception catching and calling other methods.
PointCut: PointCut are fundamentally those Joinpoints where we can put our advice(or call aspect).
Advice: This is an object which includes API invocations to the system-wide concerns representing the action to perform at a joinpoint designated by a point.

5. What are the types of Advice in Spring AOP?

In Spring AOP, 5 type of advices are fortified :
  • Before advice – Run before the method execution
  • After returning advice – Run after the method returns a result
  • After throwing advice – Run after the method throws an exception
  • Around advice – Run around the method execution, cumulate all three advices above.
  • Finally advice. Advice that is executed irrespective of how a joinpoint exits.

6. What is the Target object in Spring AOP?

The target object is an object being advised by one or more aspects. It will always be a proxy object. It is additionally referred to as the advised object.

7. What is a Proxy in Spring AOP?

AOP proxy is an object engendered by the AOP framework in order to implement the aspect contracts. In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.

8. What are the different types of AutoProxying in Spring AOP?

  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator
  • Metadata autoproxying

9. What is weaving in Spring AOP?

This is the process of inserting aspects into the application code at the appropriate point.

10. Which design pattern is used in Spring AOP?

Proxy - used heavily in AOP and remoting.
Singleton - beans defined in spring config files are singletons.
The template method is used extensively to deal with repeated boilerplate code.

11. What is @aspect annotation in spring boot?

An aspect can be engendered in spring boot with help of @Aspect annotation and registering with bean container with @Component annotation. Inside the aspect class, we can engender advices as required.

12. What are the two types of implementing Spring AOP?

  • Using XML configuration files

For Example, Declaring an aspect:

<aop:config>
<aop:aspect id="myAspect" ref="myBean">
...
</aop:aspect>
</aop:config>

<bean id="myBean" class="...">
...
</bean>

  • Using AspectJ annotation style

For Example, Declaring an aspect:

import org.aspectj.lang.annotation.Aspect;

@Aspect
public class MyAspect {

}




Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete