Aspect Oriented Programming(AOP) deals with workflow management. Concept of AOP states that workflow must be separated from processing logic because the workflow changes by the time if workflow controlled programmatically then each time workflow changes code need to be modified.
In technical term AOP is an alternative programming model of implementing cross-cutting concerns. A concern represents an independent operation which is performed as part of a task if an operation participates in multiple tasks. Then it is said to be cross-cutting.
Advantages of AOP
1.Maintenance is simplified because number or order of concerns can be changed without affecting or modifying code.
2.Productivity is increased as programmer only need to focus on business logic.
Terminology of AOP
There are following terminologies used in AOP.
Implementation of a concern is called an advice. Different types of advice include “around,”
“before” and “after” advice.
It is uniquely identifiable event in the application which can be used to associate advice to methods. Logging in class,creation of objects,method invocation etc are example of join point.
It is collection of join points.
It is collection of pointcut and advice.
It is referred to as the advised object. In Spring AOP implemented using runtime proxies, target object will always be a proxied object.
It is an object created by the Spring AOP framework in order to invoke method etc. In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
It is process of associating advice to the methods. Spring AOP supports runtime weaving.
Spring AOP Example
1.Spring AOP has following four types of advice which can be used in application.
1.Before Advice :
Spring AOP provides MethodBeforeAdvice interface to Before Advice. This interface has following method.
2.After Advice : Spring AOP provides AfterReturningAdvice interface to After Advice. This interface has following method.
3.Around Advice: In Around Advice we can perform operations before and after execution of business logic. We need to implement MethodInvocation interface to use this advice.
4.Throws Advice :Spring AOP provides ThrowsAdvice interface to After Advice. This interface has following method.
For each of these advice predefined interface is provided by framework. Implementation of these interfaces are to be provided by application programmer.
To hide the complexity of creating handler and proxy objects from end user Spring framework provides factory class by the name ProxyFactoryBean.
1.Spring Core module jars
2.Spring AOP module jars
Example of Before Advice
For Before Advice we need to implement MethodBeforeAdvice interface and then before() need to define.
The “A” class is target class which method display() we need to invoke.Before invoking display() method we want some preprocessing as below.
Example of After Advice
For After Advice we need to implement AfterReturningAdvice interface and then afterReturning() method need to define.
The “A” class is target class which method display() we need to invoke.Before invoking display() method we want some postprocessing as below.