Top 24 Spring Aop Interview Questions You Must Prepare 27.Apr.2024

In XML.

<bean class="com.doj.aop.LoggingAspect" id="loggingAspect">

<!-- configure properties of aspect here -->

</bean>

In Java

@Aspect

@Component

class LoggingAspect{

//advice

//pointcut 

}

Spring will create either JDK or CGLib proxies

JDK Proxy

  • Also called dynamic proxies
  • API is built into the JDK
  • Requirements: Java interface(s)
  • All interfaces proxied

CGLib Proxy

  • NOT built into JDK
  • Included in Spring jars
  • Used when interface not available
  • Cannot be applied to final classes or method

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.

  1. Before
  2. After
  3. AfterThrowing
  4. AfterReturning
  5. Around

  • Execution
  • This
  • Target
  • Args
  • @target
  • @args
  • @within
  • @annotation

Pointcut

An expression that selects one or more Join Points

Join Point

A point in the execution of a program such as a method call or exception thrown

Advice

Code to be executed at each selected Join Point

Aspect

A module that encapsulates pointcuts and advice

Weaving

Technique by which aspects are combined with main code

Introduction

Spring AOP allows to introduce new interfaces (and a corresponding application) to any object advises. 

Target Object

An object is assisted by one or more respects. Also known as the object advised. 

AOP Proxy

AOP proxy is an object used to perform the contract area. This object is created by the AOP framework. In Spring AOP proxy is part of JDK dynamic proxy or proxy CGLIB.

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).

After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.

After throwing advice: Advice to be executed if a method exits by throwing an exception.

After advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

A named pointcut can be declared inside an <aop:config> element, enabling the pointcut definition to be shared across several aspects and advisors.

<aop:config>

    <aop:pointcut id="businessService" expression="execution(* com.xyz.myapp.service.*.*(..))"/>

</aop:config>

  1. spring-aop.jar
  2. aspectjrt.jar
  3. aspectjweaver.jar

To use @AspectJ aspects in a Spring configuration you need to enable Spring support for configuring Spring AOP based on @AspectJ aspects, and autoproxying beans based on whether or not they are advised by those aspects.

Enabling @AspectJ Support with Java configuration

To enable @AspectJ support with Java @Configuration add the @EnableAspectJAutoProxy annotation:

@Configuration

@EnableAspectJAutoProxy

public class AppConfig {

}

Enabling @AspectJ Support with XML configuration

To enable @AspectJ support with XML based configuration use the <aop:aspectj-autoproxy/> element:

<aop:aspectj-autoproxy/>

Load-time weaving (LTW) or Run time weaving is a process of weaving AspectJ aspects into the classes of the application when the classes are being loaded in JVM.

Aspect-Oriented Programming (AOP) is another way of thing to some areas of application i.e. cross cutting concern like security, logging and transaction. AOP is simple complement of OOP programming for different concerns. In OOP, the key unit of modularity is the class, whereas in AOP the unit of modularity is the aspect.

Aspect-Oriented Programming (AOP) enables modularization of cross-cutting concerns to solve following problems.

  • To avoid tangling
  • To eliminate scattering.

Externalize the pointcut to a named pointcut. Avoid to writing complex pointcut expression across the application.

Include the below XML code in application XML

<aop:aspectj-autoproxy/>

An around advice is a special advice that can control when and if a method (or other join point) is executed. This is true for around advices only, so they require an argument of type ProceedingJoinPoint, whereas other advices just use a plain JoinPoint. ProceedingJoinPoint is used as an argument of the methods which hints for before, after, after throwing and around. ProceedingJoinPoint has the methods like getKind, getTarget, proceed etc.

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.

Implement your mainline application logic:

  • Focusing on the core problem
  • Write aspects to implement your cross-cutting concerns
  • Spring provides many aspects out-of-the-box Weave the aspects into your application
  • Adding the cross-cutting behaviors to the right places.

Context provided by the JoinPoint parameter and Context about the intercepted point.

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. 

Find the below code snippet.

@Pointcut("execution(* save(..))")

private void dataSave {}

To enable @AspectJ support with Java @Configuration add the @EnableAspectJAutoProxy annotation:

@Configuration

@EnableAspectJAutoProxy

public class AppConfig {

}

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.

  • Can only advise non-private methods
  • Can only apply aspects to Spring Beans
  • Limitations of weaving with proxies
  • When using proxies, suppose method a() calls method b() on the same class/interface
  • advice will never be executed for method b()