Monday, October 10, 2016

Spring AOP

Introduction


Aspect-Oriented Programming undefined AOP) complements Object-Oriented Programming undefined OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.
AOP entails breaking down program logic into distinct parts called so-called concerns. The functions that span multiple points of an application are called cross-cutting concerns and these cross-cutting concerns are conceptually separate from the application's business logic. 
Put it simple, it’s just an interceptor to intercept some processes, for example, when a method is execute, Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.
There are various common good examples of aspects like logging, auditing, declarative transactions, security, and caching etc.

Why use AOP?

It provides the pluggable way to dynamically add the additional concern before, after or around the actual logic. Suppose there are 10 methods in a class as given below:
class A {public void method1undefined){ authenticationundefined);//Method1’s business logic…sendNotificationundefined);}public void method2undefined){ authenticationundefined);//Method2’s business logic…sendNotificationundefined);}
}

Understanding Scenario : There are so many methods for the same kind, which first authenticate every request and in the end it send / notify the user by some notification.
Problem without AOP We can call methods undefinedthat authentication and sends notification) from the methods. In such scenario, we need to write the code in all the methods.
But, if client says in future, I don't have to send notification, you need to change all the methods. It leads to the maintenance problem.
Solution with AOP We don't have to call methods from the method. Now we can define the additional concern like authentication, sending notification etc. in the method of a class. Its entry is given in the xml file.
In future, if client says to remove the notifier functionality, we need to change only in the xml file. So, maintenance is easy in AOP.


Terminologies :

  • Aspect: a modularization of a concern that cuts across multiple classes.
  • Join point: This represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework.
  • Advice: This is the actual action to be taken either before or after the method execution. This is actual piece of code that is invoked during program execution by Spring AOP framework.
  • Pointcut: This is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples.
  • Introduction: An introduction allows you to add new methods or attributes to 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: Weaving 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.

Types of advice:
  • Before advice: Run advice before the a method execution.
  • After returning advice: Run advice after the a method execution only if method completes successfully.
  • After throwing advice: Run advice after the a method execution only if method exits by throwing an exception.
  • After undefinedfinally) advice: Run advice after the a method execution regardless of its outcome.
  • Around advice: Run advice before and after the advised method is invoked.

Spring supports the @AspectJ annotation style approach and the schema-based approach to implement custom aspects.

No comments :

Post a Comment