Top Spring AOP Interview Questions (2024)
What is Spring AOP?
What are the advantages and disadvantages of Spring AOP?
What is Aspect in Spring AOP?
What is Pointcut in Spring AOP
What is the Join point in Spring AOP?
What does it mean by Advice and its types in Spring AOP
What is the Target object in Spring AOP?
What is the weaving in Spring AOP?
What is Proxy in Spring AOP?
What are the differences between Spring AOP and AspectJ?
Where weaving can happen in the target object's lifetime in Spring AOP?
Which dependency is required to set up Spring AOP with Spring boot?
How to enable Advice in Spring AOP?
How you can enable or disable the Spring AOP based on configuration flag?
How to create Aspects with @Aspect?
How to create Before and After (finally) Advice AOP Advice in Spring Boot?
What is After Returning Advice in spring AOP?
How to create custom annotation using Spring AOP?
How to create LoggingAspect in Spring AOP?
How to create AfterThrowing AOP Advice in Spring Boot?
Q: What is Spring AOP?
Ans:
Aspect-oriented programming (AOP) is a programming technique that supports the separation of cross-cutting concerns in order to increase modularity.
AOP is typically used to implement cross-cutting concerns, which implies that it defines functionality that is required in multiple places across an application in one place. You can add new functionality before or after a method is executed like transaction management, logging or security which cut across multiple types and objects (often termed crosscutting concerns).
Q: What are the advantages and disadvantages of Spring AOP?
Ans:
Advantages of Spring AOP
- It is easy to configure.
- Spring AOP is implemented in pure Java, so separate compilation unit or separate class loader are not required.
- It utilizes Spring's IOC container for dependency injection.
- Can create aspects using
@AspectJ
annotation based or using XML based. - It integrates cross-cutting concerns into the classes,.
Disadvantages of Spring AOP
- Debugging the AOP framework-based application code is a little challenge.
- Only methods with a public visibility will be recommended, not those with a private, protected, or default visibility.
- Aspects cannot be advised by other aspects. This is because once a class is marked as an aspect (using XML or annotation), Spring prevents it from being auto-proxied.
Take a look at our suggested post :
Q: What is Aspect in Spring AOP?
Ans:
An aspect is a cross-cutting module that combines advice and pointcuts. A standard class
tagged with the @Aspect
annotation can be used to implement an aspect.
Aspects are mostly used to enable cross-cutting concerns like logging, profiling, caching, and transaction management.
Q: What is Pointcut in Spring AOP?
Ans:
A pointcut is an expression that chooses one or more join points at which advice is given. Pointcuts can be defined using expressions or patterns. It supports a number of expressions that correspond to the join points. The
Q: What is the Join point in Spring AOP?
Ans:
A join point is a place in the application where an AOP aspect is applied. It could also be a specific advice execution instance. A join point in AOP can be a method execution, exception handling, changing the value of an object variable, and so on.
Q: What does it mean by Advice and its types in Spring AOP?
Ans:
The advice is an action which we take before or after the method execution. In the Spring AOP framework, there are five types of advice: before, after, after-returning, after-throwing, and around advice. Advice is taken at a specific join point.
Q: What is the Target object in Spring AOP?
Ans:
The target object is the object to which advice is applied. Objects that are targeted are always proxied. It implies that at runtime, a subclass is built with the target method overridden and advices are included based on their configuration.
Q: What is the weaving in Spring AOP?
Ans:
It is the process of connecting aspects to other types of applications. Weaving can be done at runtime, load time, or compile time.
Q: What is Proxy in Spring AOP?
Ans:
AOP proxy is an object that is formed when advice is applied to a target object. The JDK dynamic proxy is used by Spring AOP to build proxy classes with target classes and advice invocations.
Q: What are the differences between Spring AOP and AspectJ?
Ans:
Spring AOP |
AspectJ |
A separate compilation process is not required. |
It requires the AspectJ compiler. |
Implemented in pure Java. |
Implemented using extensions of Java programming language. |
It allows only method execution pointcuts. |
It allows all pointcuts. |
It could be implemented on beans managed by Spring Container. |
It could be implemented on all domain objects. |
It allows only method level weaving. |
It could wave fields, methods, constructors, static initializers, final class, etc. |
Q: Where weaving can happen in the target object's lifetime in Spring AOP?
Ans:
The weaving can happen at many points in the target object's lifetime:
Compile Time
Aspects are woven into the target class as it is compiled. It needs the use of a unique compiler. This is how AspectJ's weaving compiler weaves aspects.Classload Time
Aspects are woven into the target class as it is loaded into the JVM. It needs the use of a special ClassLoader that improves the bytecode of the target class before it is used in the application. Load-time weaving (LTW) in AspectJ 5 allows you to weave aspects in this way.Runtime
Aspects are weaved in at some point throughout the application's execution. While weaving in the aspects, an AOP container will dynamically build a proxy object that will delegate to the target object.
Q: Which dependency is required to set up Spring AOP with Spring boot?
Ans:
The spring-boot-starter-aop
dependency must be included when introducing AOP in
spring boot. Spring-aop
and aspectjweaver
dependencies are imported
into the application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Q:How to enable Advice in Spring AOP?
Ans:
You can use AspectJ annotations to declare advice in Spring, however first you should add the
@EnableAspectJAutoProxy
annotation to your configuration class, which enables
support for handling components marked with AspectJ's @Aspect
annotation.
@Configuration
@EnableAspectJAutoProxy
public class SpringAopConfiguration {
...
}
Q: How you can enable or disable the Spring AOP based on configuration flag?
Ans:
When the maven dependencies are imported, AopAutoConfiguration
is triggered,
enabling AOP via the @EnableAspectJAutoProxy
annotation.
If spring.aop.auto = false
in application.properties
, the auto
configuration will not be activated.
spring.aop.auto = false //it disables Spring AOP
Q: How to create Aspects with @Aspect?
Ans:
In spring boot, an aspect can be created using the @Aspect
annotation and
registered with a bean container using the @Component
annotation.
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectExample
{
// @Before , @AfterReturning , @AfterThrowing , @After, @Around
// method to execute
}
Q: How to create Before and After (finally) Advice AOP Advice in Spring Boot?
Ans:
@Before
Advice that runs before a join point but has no ability to stop the execution flow from continuing to the join point (unless it throws an exception).
@After -After (finally)
After advice is executed after the join point method completes its execution, whether properly
or by throwing an
exception. Using the @After
annotation, we can create after-advice.
We can use aspectj @Before
and @After
annotation advice to call before and after method execution.
Refer Spring Boot + AOP + Before After Advice Example
Q: What is After Returning Advice in spring AOP?
Ans:
There are situations when we want advice methods to only run if the join
point method runs normally. To indicate a method as after returning advice, we can use the
@AfterReturning
annotation.
Q: How to create custom annotation using Spring AOP?
Ans:
We can make utilise of Spring AOP can implement custom annotations at the method level in
a
Spring Boot application.
Refer Spring Boot + Custom Annotation using AOP Example
Q: How to create LoggingAspect in Spring AOP?
Ans:
We can use AspectJ (aop advice) to measure method execution time as
a logging mechanism in Spring Boot applications to handle cross-cutting problems.
Refer Spring Boot + AOP + LoggingAspect Example
Q: How to create AfterThrowing AOP Advice in Spring Boot?
Ans:
AspectJ @AfterThrowing
advice is executed after a join point fails to complete
normally and end up throwing an exception.
As per below code we can handle AfterThrowing Advice.
package com.techgeeknext.aop;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AfterThrowingAspectExample {
private static final Logger LOGGER = LogManager.getLogger(AfterThrowingAspectExample.class);
@AfterThrowing (pointcut = "execution(* com.techgeeknext.service.employeeServiceImpl.*(..))", throwing = "ex")
public void logAfterThrowingAllMethods(Exception ex) throws Throwable
{
LOGGER.info("======TECHGEEKNEXT - Spring Boot AOP AfterThrowing Advice Example ");
LOGGER.info("=====Exception " + ex);
}
}