Spring AOP Interview Questions List in 2020

Today, in this article, we are providing you a comprehensive list of Spring AOP interview questions, which are typical to be asked in an interview. This ensures full-coverage on Spring AOP questions.

Current article is part of our series on Spring Interview Questions.

Even if you don’t have any hands-on experience on Spring AOP, by going through this article you will have pretty good idea about it, at-least enough to clear the interview.

To accomplish this, we highly recommend you to firstly go through this video by Java Brains to understand the Spring AOP fundamentals. If you already know it, you can skip it but if have got time, you must refresh your knowledge.

With this, you are prepared to go through interview questions. So, without further ado, let’s go through the questions and answers list.

Spring AOP Interview Questions And Answers

Spring AOP Interview Questions And Answers

1. What is AOP or Aspect Oriented Programming in Spring framework?

When developing an application, some program logic is common across multiple components and modules.

Most popular example for such kind of scenarios is: Logging. Maybe, we want to log the entry and exit of a method in program flow.

Other example could be: We want to authenticate every HTTP request coming to the server.

These common concerns which cut across multiple components is called cross-cutting concerns or an Aspect of an application.

Instead of applying an Aspect to each component which will create duplication, scattered piece of code to manage which has nothing to do with actual business logic, Spring provides us API to program around these Aspects.

Aspect Oriented Programming (AOP)

This is called Aspect oriented programming.

2. What is the difference between concern vs cross-cutting concern in Spring AOP?

A concern might be related to one or more components or modules.

If a concern is specific to a Single module, it is just called an application Concern. Typically, these are related to business domain of an application.

On that contrary, if a concern is applicable across two or more modules, it is called cross-cutting concern. Typical examples includes Logging, Transaction Management.

Spring AOP Cross-Cutting Concern

3. What are some of the Spring AOP advantages?

  1. Modularity: It helps us to centralize the cross-cutting concerns.
  2. Easier Maintenance: It is easier to make changes at one place.
  3. Pluggable: It gets easy to add or remove aspects.

This is one of the must know Spring AOP interview questions.

4. How do you implement cross-cutting concerns in a web application?

We can do this with the help of Spring AOP.

Now-a-days, we can directly do it in Spring Boot AOP.

5. When to use Spring AOP?

There are some typical use cases where we use Spring AOP. For example:

  1. Application Logging
  2. Spring Security configuration
  3. Transaction Management maybe.

Other than that, wherever we want to achieve some kind of cross-cutting concern, we implement that with the help of Spring AOP.

6. How Spring AOP works?

We configure Spring AOP separately from main application logic.

In this configuration, we set 2 things on broad prospective:

  1. What to do?
  2. and where to do?

To define these Spring provides us different concepts and components which we can use. For example: Aspect, JoinPoint, PointCut.

Once configured, it applies the defined logic in the declared locations.

How Spring AOP works - JoinPoints and PointCuts

Note: Till now we discussed the introductory Spring AOP interview questions. Now, we will start with the internals of Spring AOP concepts.

7. What is Aspect in Spring AOP?

A cross-cutting concern of an application which needs be programmed in a modular way, in separate to application’s business logic, is called an Aspect.

For example: Logging

8. How to declare a Spring AOP Aspect?

@Aspect annotation is used to declare an Aspect like this:

@Aspect
public class OurAspect{
}

We can also do the same with XML configuration like below:

<bean id="ourAspect" class="path.to.OurAspect">
</bean>

9. Can @Aspect classes be automatically scanned with component scannning?

No.

We need to make it annotate with @Component and then only it will be eligible for component scanning.

For example:

@Component
@Aspect
public class OurAspect{
}

10. What is Advice in Spring AOP?

Advice is the logic we write for an Aspect.

For example: Log the method name could be an Advice for the Logging Aspect.

In terms of programming, it is achieved through writing a method for each Advice.

11. What are different Spring AOP Advice types?

list of spring aop advice types

Below are different Advice types supported by Spring:

  1. Before Advice: It will run before the JoinPoint execution starts.
  2. After Returning Advice: It will run after a JoinPoint execution is completed normally.
  3. After throwing Advice: It will run if a JoinPoint throws Exception.
  4. After Advice: It will run regardless of JoinPoint executes normally or with Exception. It is analogous to finally block.
  5. Spring AOP Around Advice: It wraps the JoinPoint. It is special in the sense that it can return the result itself and choose to stop the execution to proceed to the JoinPoint.

12. How to declare an Advice in Spring AOP?

Here is an example of Before Advice:

@Component
@Aspect
public class OurLoggingAspect{

    @Before("PointCutExpression") 
    public void log(JoinPoint joinPoint)
    {
    }
}

So, log method is what will get executed as advice, before the method(s), which matches PointCut expression.

13. Is there any recommended way to select any one of the advice types?

Yes. The general consideration is that:

we should pick least powerful advice type if we can accomplish our work with that.

For example:

If our task in hand can be accomplished with Before Advice or After Advice, then we need not use Around Advice.

By using the most specific advice, our program will be less prone to errors.

14. What is a JoinPoint in Spring AOP?

JoinPoint is the target location in the application where advice (method) will be applied.

In Spring AOP, a JoinPoint is always a method.

15. Does Spring framework support all JoinPoints?

A JoinPoint could be a method execution, handling of an exception or handling of an event.

Spring AOP supports only method execution JoinPoint.

If we want joinpoints on fields, then we can use AspectJ support.

16. What is Pointcut in Spring AOP?

We have Advice which needs to be executed and we have whole application’s classes’s methods as possible JoinPoints.

Now, where exactly the Advice will be executed? For which set of methods in the whole application?

The answer to this is: PointCut.

PointCut is a regular expression(regex).

We associate a PointCut expression with each Advice. This expression will be applied to each method in the execution flow.

If it returns true, Advice will be applied for that method and if it returns false, it will not.

17. What is the difference between JoinPoint and Pointcut?

Refer this for detailed explanation.

This could be one of the most confusing Spring AOP interview questions sometimes.

18. Could you give an example of PointCut expression?

Yes sure.

@Component
@Aspect
public class OurLoggingAspect{

    @Before("execution(* handleItem(..))") 
    public void log(JoinPoint joinPoint)
    {
    }
}

This PointCut expression will be true for all method named as handleItem and because of this, Advice in log method will be executed before handleItem method.

19. How to implement logging using Spring AOP?

Refer to code example in above question.

If there are multiple advices for logging, here is how it will work:

working of advice chain in logging

20. What is “execute” in Pointcut expression?

“execute” is called as PointCut Designator.

We can use any of the AspectJ PointCut Desidnators (PCD), and execute is just one of them.

21. What are other PointCut Designators?

We have execution, within, target, args, this as PointCut Designators in Spring AOP.

We have annotated variation too: @target, @args, @within and @annotation.

All these comes from AspectJ framework which we use it in Spring AOP framework.

22. Can a Spring AOP pointcut have multiple expressions? Give example.

Yes. Example being:

expression="execution(* package1.*.*(..)) || execution(* package2.*.*(..))"

Here we are using boolean operator to add multiple expressions in a single Spring AOP pointcut.

23. Write a pointcut for all public methods.

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

24. Write a pointcut for all private methods.

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

25. What are Advice arguments?

We can use args() expression, to get the JoinPoint arguments in the Advice, to customize the Advice to further level.

It will run only for JoinPoints whose arguments matches the args() expression.

26. What is target object?

The object for which one or more Advice will be executed. It is also referred as Advice object.

27. What is Introduction in Spring AOP?

Introduction is a concept by which we can enhance a target object’s class in one way or another, without even touching it.

We can implement an Interface, provide it’s concrete implementation, add additional methods or attributes of the target object’s class, in the Aspect itself, in a modular fashion.

28. What is Weaving?

How Weaving works explained

Weaving is a process of linking an Aspect with other application objects to create the target object.

Now, let’s revise the key Spring AOP terminologies:

Spring AOP terminologies

Note: From here starts some of the trickiest of Spring AOP interview questions. So, let’s pay attention.

29. When does Weaving happen in Spring AOP?

Weaving can be done at compile time, load time or run-time.

Spring AOP does it at runtime. AspectJ does it at compile-time.

30. What is Spring AOP proxy object?

Spring implement AOP contracts with the help of proxy objects.

These are the objects created at runtime to apply the Advice.

By default, these are JDK dynamic proxies.

If target object does not implement any interface, Spring AOP uses CGLIB proxy.

Here is the issue which you might face while approaching Introduction with Spring AOP Proxy.

Spring AOP Proxy example

31. What are different AOP implementations available?

  1. Spring AOP – Spring’s own implementation.
  2. AspectJ – now an Eclipse foundation project.
  3. JBoss AOP – maintained by JBoss community.
popular aop framework implementations

32. Give a Spring AOP example.

This article on Journaldev has a very good summarized example on Spring AOP.

33. What is the difference between Spring AOP vs AspectJ?

Spring AOP takes care of small set of regular and typical use-cases whereas AspectJ is the most comprehensive one.

Spring AOP supports only method Pointcuts whereas AspectJ supports field level too.

Spring AOP takes care of the run-time weaving process whereas in case of AspectJ, we need to trigger the weaving process with the help of tools provided, at compile time.

34. How Spring AOP works with AspectJ?

Spring AOP uses AspectJ’s pointcut and matching library to support @AspectJ style of programming. It does not use the AspectJ compiling or weaving.

35. What is the name of AspectJ pointcut and matching library?

aspectjweaver.jar

This jar has to be in our classpath to support AspectJ in a Spring AOP application.

36. How to enable AspectJ in a Spring AOP application?

With XML namespace:

<aop:aspectj-autoproxy/>

With Java Configuration:

@Configuration
@EnableAspectJAutoProxy
public class CustomAOPConfig {

}

Spring Boot AOP Interview Questions

37. How to enable Spring Boot AOP?

  1. Add ” spring-boot-starter-aop” starter project dependency.
  2. Enable or disable AopAutoConfiguration in application.properties file. For example: spring.aop.auto=false. (It is true by default)

Our application is now ready as a Spring Boot AOP application.

38. Give me a Spring Boot AOP example.

Writing an AOP application with Spring Boot it not different than typical Spring application. We just need to enable Spring Boot as stated earlier.

Although DZone and javatpoint has examples for this but checkout this example which uses starter project.

39. How to write Spring AOP custom annotation? Give example.

Firstly, create a custom annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation{
}

The method for which we want to apply the aspect, annotate it with CustomAnnotation.

@CustomAnnotation
public void aMethod() {
//some logic
}

Now create a pointcut like this:

@Before("@annotation(CustomAnnotation)")

40. Write a Spring AspectJ example for audit logging.

Checkout this article which does audit logging using AspectJ with Spring AOP. It uses Gradle as build system, not Maven.

By this, we are finished with the exhaustive list of Spring AOP interview questions. Hope, this should be enough.

If you still got some time then checkout this video by “in28Minutes” on Spring AOP explaining Spring AOP with examples.

What to read next?

Spring AOP is a complementary topic to the Spring IoC and Spring MVC, which are again part of Spring core framework. Hence, it is highly recommended to go through: Spring MVC Interview Questions or Spring Boot Interview Questions.

If you have already gone through them, then checkout our Spring Interview Question article and pick the topic which suits your interview profile.

Best of luck!!

Leave a Comment