March 08, 2018

#SpringAOP Part 1: Aspect Oriented Programming Interview Questions & Answers

What is Aspect Oriented Programming?
Aspect-oriented programming or AOP is one of the feature provided by Spring, actually it's not just a feature it a model of programming itself.

Suppose we have a common procedure across different objects, e.g Log message. To implement this, we can have a logMessage() method in all the objects. But this is not really a good design, because we are repeating the same method in all the objects. What we can do, we will create a new Logger class, which has logMessage() method. All other objects will refer to the Logger object's logMessage() method to print the message.

Now, if you are making a design diagram which depicts the relationship between all the objects and you want to know which object is an important one (by checking their relationship with other objects). In such case, the Logger will be the most important object, although it's not adding any business value.

The problem with this approach is that there will be too many relationships to the crosscutting objects and we need to still write the code to call the logMessage() method in all the business objects. In case, we use a different log implementation, then we need to make changes in all the business objects and make the changes accordingly to call the new log method.

All the applications have one other cross cutting concerns like logging, security or transactions. With AOP, we can remove the cross cutting concerns.

With AOP, we will create a 'Logging Aspect' class. After creating the aspect, we do not reference it from the objects, instead we will define the 'aspect configuration' which tells to which objects or methods these aspects apply to.

Suppose we have three business objects Partner, Vendor and Customer, each having the create() method and we need a logging mechanism while creating the Vendor and Customer. We need to do 'aspect configuration', Spring will make sure to call the logging method before creating the Vendor or a Customer. In case you decide to change the logging mechanism, all you need to make changes in 'aspect configuration'.

We can configure aspect to run before or after a particular (target) method.

Steps we need to take to write Aspect oriented program:
  • Write Aspects.
  • Configure where the aspects apply.
Explain different AOP terminologies?
  • Aspect: This is a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. Aspects are implemented using regular classes or regular classes annotated with the @Aspect annotation in Spring Framework.
  • Advice: It's the actual action to be taken either before or after the method execution. Spring AOP uses an advice as an interceptor, maintaining a chain of interceptors “around” the join point.
  • Join Point: JoinPoint is the target location in the application where advice (method) will be applied. In Spring AOP, a JoinPoint is always a method. 
  • Pointcut: This is a set of one or more join points where an advice should be executed. 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.
  • Introduction: This allows you to add new methods or attributes to the existing classes.
  • Target object: The object being advised by one or more aspects. This object will always be a proxied object, also referred to as the advised object.
  • Weaving: It is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime.
What do you mean by Aspect?

  • Aspect is a modularization of concern which cuts across multiple objects.
  • Aspects are implemented using regular classes or regular classes annotated with the @Aspect annotation in Spring Framework.
  • Aspects are reusable blocks of code that are injected into application at runtime.
  • These are very powerful tools for adding behavior, especially behavior that applies to cross-cutting concerns. We can write the code in one spot and automatically apply it to many locations throughout our application.

What are the common applications of aspects in application development?

  • Logging
  • Security
  • Transaction management is another common use of aspects across the applications base. Spring also provides an aspect @Transactional to do this work.
  • Caching is another very useful opportunity for aspects and for this Spring provides an annotation. We can also write our own custom caching behavior.
What is an Advice?
An action taken by an aspect at a particular joinpoint is known as an Advice. Spring AOP uses an advice as an interceptor, maintaining a chain of interceptors “around” the join point. e.g: Log the method name could be an Advice for the Logging Aspect.



What are the different types of Advices?

  • Before: These types of advices execute before the joinpoint methods and are configured using @Before annotation mark.
  • After returning: These types of advices execute after the joinpoint methods completes executing normally and are configured using @AfterReturning annotation mark.
  • After throwing:  These types of advices execute only if joinpoint method exits by throwing an exception and are configured using @AfterThrowing annotation mark.
  • After (finally): These types of advices execute after a joinpoint method, regardless of the method’s exit whether normally or exceptional return and are configured using @After annotation mark.
  • Around: These types of advices execute before and after a joinpoint and are configured using @Around annotation mark.
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 JoinPoint.

What is the difference between a Joint point and Point cut?
Join point is a point of execution of the program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

Pointcut is a predicate or expression that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut.

In short, advice represents an action taken by an aspect at a particular joint point. A joint point is a point in a program, such as method execution, exception handling etc, where as a pointcut is an expression language of AOP that matched joint points. 

What is Weaving?
Weaving is the process for interleaving separate cross-cutting concerns such as logging into core concerns such as business logic code to complete the system. AOP weaving composes different implementations of aspects into a cohesive system based on weaving rules. The weaving process (aka injection of aspects into Java classes) can happen at:
  • Compile-time: Weaving occurs during compilation process.
  • Load-time: Weaving occurs at the byte-code level at class loading time.
  • Runtime: Similar to load-time where weaving occurs at byte-code level during runtime as join points are reached in the executing application.
In Spring AOP, weaving is performed at runtime.

On what basis should we decided to use a particular weaving approach?
Load-time and runtime weaving have the advantages of being highly dynamic and enabling changes on the fly without having to rebuild and redeploy. But Load-time and runtime weaving adversely affect system performance. Compile time weaving offers better performance but requires rebuilding and redeployment to effect changes. 

What is the difference between concern and cross-cutting concern in Spring AOP?
A Concern is a behavior, which we want to have in our application like Employment Management. Whereas cross-cutting concern is a concern which is applicable throughout the application e.g logging and security.

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.

What are the available AOP implementations?
AspectJ, Spring AOP and JBoss AOP.

How to enable @AspectJ Support?
Include the below XML code in application XML: < aop:aspectj-autoproxy/ >
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. In simpler words Spring AOP supports only method level pointcuts, where as AspectJ supports field level pointcuts
  • One of the shortcoming of Spring AOP is that it can be applied only to the beans created through Spring Context.
  • 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.
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 compiler in the build process. In case if we advice objects not to be managed by Spring Container, use AspectJ.

What are the required libraries to run AspectJ LTW in Spring?
spring-aop.jar, aspectjrt.jar and aspectjweaver.jar
 
What do you mean by Proxy in Spring Framework?
An object which is created after applying advice to a target object is known as a Proxy. In case of client objects the target object and the proxy object are the same.

Which design pattern is used in Spring AOP?
AOP is mainly based on Proxy design pattern (to perform run-time weaving). It also uses remote, Singleton, template method and decorator design pattern.

What are the advantages of Spring AOP?

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

When does Weaving happen in Spring AOP?
Weaving can be done at compile time, load time or run-time. Spring AOP do it at runtime. AspectJ do it at compile-time.

What is AOP proxy object?
It is an object that is created after applying advice to the target 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.

-K Himaanshu Shuklaa..

No comments:

Post a Comment