Spring AOP Interview Questions

What is Aspect Oriented Programming (AOP) in Spring?

Aspect Oriented Programming works like Object Oriented Programming. In Object Oriented Programming, the unit of modularity is Object But in Aspect Oriented Programming the unit of modularity is Aspect. Aspect works as the modularization of concerns known as crosscutting concerns in AOP. AOP framework is pluggable in spring. AOP provides declarative enterprise service and allows users to implement custom aspects.

Define AOP terminologies in Spring.

Aspect: In multiple classes, the modularization of concerns that acts as crosscutting concerns. Example: Transaction management
Join Point: Join Point is a point during the execution of the method.
Advice: At a join point, the action taken by aspect is Advice.
Pointcut: Those predicates which matches join point is called Pointcut.
Weaving: Other application type can be linked with aspect and that is known as weaving.
Introduction: Introduction is defining additional methods fields for a type.
Target object: Those objects which are advised by aspects are Target Object.
AOP proxy: AOP framework creates an object to meet aspect contract, that object is AOP proxy.

What is Aspect, Advice, Pointcut, JointPoint and Advice Arguments in AOP?

Aspect: Aspect is a class that implements cross-cutting concerns, such as transaction management. Aspects can be a normal class configured and then configured in Spring Bean configuration file or we can use Spring AspectJ support to declare a class as Aspect using @Aspect annotation.

Advice: Advice is the action taken for a particular join point. In terms of programming, they are methods that gets executed when a specific join point with matching pointcut is reached in the application. You can think of Advices as Spring interceptors or Servlet Filters.

Pointcut: Pointcut are regular expressions that is matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points. Spring framework uses the AspectJ pointcut expression language for determining the join points where advice methods will be applied.

Join Point: A join point is the specific point in the application such as method execution, exception handling, changing object variable values etc. In Spring AOP a join points is always the execution of a method.

Advice Arguments: We can pass arguments in the advice methods. We can use args() expression in the pointcut to be applied to any method that matches the argument pattern. If we use this, then we need to use the same name in the advice method from where argument type is determined.

What is the difference between Spring AOP and AspectJ AOP?

AspectJ is the industry-standard implementation for Aspect Oriented Programming whereas Spring implements AOP for some cases. Main differences between Spring AOP and AspectJ are:

  • Spring AOP is simpler to use than AspectJ because we don’t need to worry about the weaving process.
  • Spring AOP supports AspectJ annotations, so if you are familiar with AspectJ then working with Spring AOP is easier.
  • Spring AOP supports only proxy-based AOP, so it can be applied only to method execution join points. AspectJ support all kinds of pointcuts.
  • One of the shortcoming of Spring AOP is that it can be applied only to the beans created through Spring Context.

What are the the types of advice in Spring AOP.

In Spring AOP, types of advice are

Before: Advice that runs before a join point.
After returning: Advice that runs after a join point normal completion.
After throwing: Advice which runs when a methods exits by throwing an exception.
After: Advice that runs after the join point exit by any way.
Around: Advice that runs surrounding to join point. Example method invocation.

How to enable @AspectJ Support?

Include the below XML code in application XML

<aop:aspectj-autoproxy/>

How to declare aspect in Spring AOP?

Using the below XML snippet

<bean id="myAspect" class="com.javaconnect.MyAspect">
   <!-- configure properties of aspect here -->
</bean>

How to declare a pointcut in Spring AOP?

Find the below code snippet.

@Pointcut("execution(* update(..))")
private void accountUpdate {}

What are the supported AspectJ pointcut designators in Spring AOP?

Followings are the AspectJ pointcut designators in Spring AOP. Execution
This
Target
Args

@target
@args
@within
@annotation

When to use Spring AOP and when to use full AspectJ?

If we only need to advice the execution of operations on Spring beans then we should use Spring AOP. Spring AOP is simpler than AspectJ. Full AspectJ requires the AspectJ complier in the build process.
In case if we advice objects not to be managed by Spring Container, use AspectJ.

What do you understand by Load-time weaving (LTW) in Spring?

Load-time weaving (LTW) is a process of weaving AspectJ aspects into an applications class file when the classes are being loaded in JVM.