1.What is meant by 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.
2.Why is it uses?
Suppose we want to log every method entry and exit. This can be achieved by writing log statements in every method at the start and end. But this will require lot of code work. There are various such tasks like Security which need to be applied across all methods or classes. These are known as cross cutting concerns. AOP addresses the problem of cross-cutting concerns, which would be any kind of code that is repeated in different methods and cannot normally be completely refactored into its own module, like with logging or verification.
3.What are the different implementations of Spring AOP ?
The different implementations of Spring AOP are-
4.Explain different AOP terminologies??
The different AOP terminologies are
Joinpoint: A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.
Advice: This is an object which includes API invocations to the system wide concerns representing the action to perform at a joinpoint specified by a point.
Pointcut: A pointcut defines at what joinpoints, the associated Advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework. Of course, you don’t want to apply all of your aspects at all of the possible joinpoints. Pointcuts allow you to specify where you want your advice to be applied. Often you specify these pointcuts using explicit class and method names or through regular expressions that define matching class and method name patterns. Some AOP frameworks allow you to create dynamic pointcuts that determine whether to apply advice based on runtime decisions, such as the value of method parameters.
Aspect:The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects.
Weaving:In Spring AOP makes it possible to modularize and separate logging, transaction like services and apply them declaratively to the components Hence programmer can focus on specific concerns. Aspects are wired into objects in the spring XML file in the way as JavaBean. This process is known as ‘Weaving’.
5.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.
6.What are the different types of Spring Advice ?
The different types of Spring Advice are-
Before advice : Advice that executes before a join point.
After returning advice : Advice to be executed after a join point completes normally.
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.
Around advice : Advice that surrounds a join point such as a method invocation.
7.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.