Most frequently asked Spring AOP Interview Questions
- Describe Spring AOP?
- What are the advantages of Spring AOP?
- What is AOP or Aspect Oriented Programming in Spring framework?
- What is named pointcut?
- What are the types of advice in spring?
- Difference between Spring AOP and AspectJ?
- Name three Typical Cross Cutting concerns?
- Ways to implement AOP?
- What is a Proxy in Spring AOP?
- What Is A Proceedingjoinpoint?
- What is AOP Alliance?
- Define Run-time AOP vs Compile-time AOP ?
- Limitations Of Spring Aop?
Describe Spring AOP?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.
Aspect Oriented Programming (AOP) compliments OOPs in the sense that it also provides modularity.AOP breaks the program logic into distinct parts (called concerns). It is used to increase modularity by cross-cutting concerns.A cross-cutting concern is a concern that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc.
What are the advantages of Spring AOP?
- AOP is non-invasive.
- AOP is implemented in pure Java.
- It uses Spring's IOC container for dependency injection.
- Like any other AOP framework, it weaves cross-cutting concerns into the classes, without making a call to the cross-cutting concerns from those classes.
- Centralizes or modularizes the cross-cutting concerns.
- Easy to configure.
What is AOP or Aspect Oriented Programming in Spring framework?
- AspectJ: It is an extension for Java programming created at PARC research centre. It uses Java like syntax and included IDE integrations for displaying crosscutting structure. It has its own compiler and weaver, on using it enables the use of full AspectJ language.
- JBoss: It is an open source Java application server developed by JBoss, used for Java development.
- Spring: It uses XML based configuration for implementing AOP, also it uses annotations which are interpreted by using a library supplied by AspectJ for parsing and matching.
What is named pointcut?PointCut is a set of one or more JoinPoint where an advice should be executed. You can specify PointCuts using expressions or patterns as we will see in our AOP examples. In Spring, PointCut helps to use specific JoinPoints to apply the advice.The pointcut language is a tool that allows joinpoint matching. A pointcut expression determines in which joinpoint executions of the base system an advice should be invoked. Pointcuts are used primarily by advice. They can be composed with boolean operators to build up other pointcuts.
What are the types of advice in spring?An important term in AOP is advice.
In Spring AOP, 5 type of advices are supported :
- Before advice
- After returning advice
- After throwing advice
- Around advice
Difference between Spring AOP and AspectJ?Spring AOP simpler to use than AspectJ, since you don't have to use LTW (load-time weaving) or the AspectJ compiler.
It uses the Proxy pattern and the Decorator pattern
Spring-AOP cannot add an aspect to anything that is not created by the Spring factory
AspectJ supports all joinpoints. This means you can do anything.
There is less runtime overhead than that of Spring AOP.
Aspects aren't applied when calling another method within the same class.
Name three Typical Cross Cutting concerns?Logging
Ways to implement AOP?
What is a Proxy in Spring AOP?Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.
If you want to force the use of CGLIB proxying you can do so. For example, to proxy every method defined for the target object, not just those implemented by its interfaces.
What Is A Proceedingjoinpoint?ProceedingJoinPoint is used as an argument of the methods which hints for before, after, after throwing and around.
What is AOP Alliance?AOP alliance is an open-source project which is aimed at promoting adoption of AOP. AOP Alliance is a set of interfaces that multiple frameworks implement including both Guice and Spring.It was chosen for Guice because it has a high capability and a simple API.
Define Run-time AOP vs Compile-time AOP ?The different types of AOPs depending on when they are loaded are- Source code weaving: Aspect code is injected as source code statements into your application source code. This is some kind of preprocessor approach. No AOP framework in the Java world uses this approach nowadays, but there used to be some in the early days of AOP.
Compile-time weaving: Aspect code is woven into your application by a special compiler.
Binary weaving: Aspect code is woven into existing class files after compilation rather than during compilation.
Load-time weaving (LTW): A weaving agent/library is loaded early when your VM/container is started. It gets a configuration file with rules describing which aspects should be woven into which classes.
Proxy-based LTW: This special LTW form is used by Spring AOP while AspectJ does the previous 3 forms listed above. It works by creating dynamic proxies for aspect targets.
Limitations Of Spring Aop?
- 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()