Skip to content
Vishnu Garg edited this page Aug 4, 2018 · 6 revisions

AOP

What is AOP

AOP is Aspect Oriented Programming. What it does is it wraps the original object by a proxy object and that proxy object is injected into user classes as dependency injection.it will look like that we are working with original object but it is not right. We work with proxy object. The proxy object is created using java.lang.reflect.InvocationHandler and java.lang.reflect.Proxy.

Brief points are

  • Aspect Oriented Programming (AOP) supplements Object Oriented Programming (OOP) by providing another way of thinking about program structure.
  • The key unit of modularity in OOPs is the class—in AOP the unit of modularity is the aspect.
  • Aspects help to modularize cross-cutting concerns, which means cross-cutting concerns can be described by any functionality that affects multiple types and objects.
  • One key component of Spring is the Aspect Oriented Programming (AOP) framework.
  • While Spring IoC container doesn't depend on the AOP framework, this means you don't need to use AOP if you do not want to—AOP supplements Spring IoC to provide a capable middleware solution.

AOP Concepts

1.Join Points: A Join Point is a point in the 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.

2.Pointcuts: Pointcuts are expressions that are matched with Join points to determine whether advice needs to be executed or not. This is a set of more than one Join point where an advice should be executed.

3. Advice: Advice defines both the when and where of an aspect. The actual action to be taken before or after method execution. Actual piece of code that is invoked during program execution by Spring's Aspect Oriented Programming framework.

4. Aspects: An aspect is the merger of Advice and Pointcuts. Advice and pointcuts define everything known about an aspect, what it does and where and when it does it.

5. Introductions: An Introduction allows adding new methods or attributes to existing classes. The new method and instance variable can be introduced to existing classes without having to change them, giving them new state and behavior.

6. Target object: object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.

7. AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.

8.Weaving: Weaving is the process of applying aspects to a target object to create a new proxied object. The target object at the specified Join points.

The weaving can occur at several points in the target object’s lifetime: Compile Time: Aspects are woven in as the target class is compiled. It requires a special compiler. AspectJ’s weaving compiler weaves aspects this way. Classload Time: Aspects are woven in as the target class is loaded into the JVM. It requires a special ClassLoader that enhances that target class's bytecode before the class is introduced into the application. AspectJ 5’s load-time weaving (LTW) support weaves aspects this way. Runtime: Aspects are woven in sometime during the execution of the application. An AOP container will dynamically generate a proxy object that will delegate to the target object while weaving in the aspects.

AOP Advice Types

  • Before Advice: These advices runs before the execution of join point methods. We can use @Before annotation to mark an advice type as Before advice.
  • After (finally) Advice: An advice that gets executed after the join point method finishes executing, whether normally or by throwing an exception. We can create after advice using** @After** annotation.
  • After Returning Advice: Sometimes we want advice methods to execute only if the join point method executes normally. We can use @AfterReturning annotation to mark a method as after returning advice.
  • After Throwing Advice: This advice gets executed only when join point method throws exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing annotation for this type of advice.
  • Around Advice: This is the most important and powerful advice. This advice surrounds the join point method and we can also choose whether to execute the join point method or not. We can write advice code that gets executed before and after the execution of the join point method. It is the responsibility of around advice to invoke the join point method and return values if the method is returning something. We use @Around annotation to create around advice methods.

Add added **aspectjrt **and **aspectjtools **dependencies.

Source code example added. AspectJ

Refrences

Clone this wiki locally