Spring AOP
hi
hi
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Aspect Oriented programming in <strong>Spring</strong>: In <strong>Spring</strong> environment Aspects are nothing but<br />
Middleware services. <strong>Spring</strong> <strong>AOP</strong> is methodology of applying Middleware services on our<br />
<strong>Spring</strong> applications where Middleware services will be developed separately and will be linked<br />
with <strong>Spring</strong> Beans either through Annotations or XML files. Middleware services are additional<br />
and optional logics that can be applied on our applications to make our applications running<br />
smoothly and perfectly.<br />
EX: logging (keeps track of the application flow of execution)<br />
Transaction Management applies do ever thing and nothing principle on sensitive logics.<br />
Q) What is the difference between OOP and <strong>AOP</strong>?<br />
<strong>Spring</strong> <strong>AOP</strong> is no way related with OOP because OOP is methodology of creating programming<br />
languages. <strong>AOP</strong> is the methodology of applying middleware services on the <strong>Spring</strong> applications.<br />
Logging Service<br />
Transaction Management<br />
Service<br />
Security Service<br />
Project1 Project2 Project3<br />
Module1 (Student Module)<br />
Module2 (Faculty Module)<br />
Module3 (Admin Module)<br />
In <strong>Spring</strong> <strong>AOP</strong> Middleware services are also called as Aspects or cross cutting concerns because<br />
they reside outside the application logics but will executed along with the application logics.<br />
For example scenario that talks about need of <strong>Spring</strong> <strong>AOP</strong> refer 100 to 102<br />
Instead of writing Middleware service logics directly with application code it is<br />
recommended to separate them from application code and apply them on application code using<br />
XML, Annotations by taking the support of <strong>Spring</strong> <strong>AOP</strong>.<br />
<strong>AOP</strong> Terminologies:<br />
1. Aspect
2. Advice<br />
3. Advisor<br />
4. Point cut<br />
5. Wiring<br />
6. Weaving<br />
7. Joint Point<br />
8. Target Object<br />
9. Proxy Object.<br />
Logging Security Transaction management<br />
(Advice1) (Advice2) (Advice3)<br />
Point Cut Point Cut Point Cut Point Cut<br />
Business Method Excution<br />
JP1 JP1 JP1<br />
Here JP represents Joint Point<br />
1. Aspect: Contains the plan of implementing Middleware Service<br />
2. Advice: It is the practical implementation of Middleware Services. The position in<br />
business methods where advices are configured are called as Joint Point<br />
EX: Beginning of the business methods, end of the business methods. Exception is resided in<br />
the business method and etc., The XML or Annotation configurations that are used to link<br />
advices with Joint Points are called Point Cuts.<br />
3. Advisor: It is a combination of Advisor + Joint Point + Point Cut.
4. Wiring: The process of configuring bean properties for dependency injection is called<br />
the process of configuring advices/Middleware services/Aspects on bean class business<br />
methods is called as Weaving. The bean class object which we are planning to apply<br />
Middleware services (but not apply) is called Target object.<br />
Note: All <strong>Spring</strong> Bean objects are Target Objects by default.<br />
Proxy Objects: It is <strong>Spring</strong> Bean object on which Middleware services are applied. If we<br />
call business methods on Target object only business logic will be executed. If we call<br />
business methods on Proxy Object the business logic will be executed along with<br />
Middleware services. Generally we never apply Middleware services in small scale project<br />
development. While we apply Middleware services in medium and large scale projects.<br />
1. Programmatic Approach: keeping the logic of applying Middleware services as Java<br />
statements directly in the applications (not recommended).<br />
EX: problem scenario given in page no: 100<br />
2. Declarative Approach: Middleware services will be applied on the applications either<br />
through XML statements or through Annotations.<br />
EX: All <strong>Spring</strong> <strong>AOP</strong> applications<br />
There is possibility of developing four types of advices those are<br />
i. BeforeAdvice (Executes at the beginning of the business method)<br />
ii. AfterAdvice (executes at the end of the business method)<br />
iii. ThrowsAdvice (executes when the business method throws an exception)<br />
iv. Around Advice ( executes at beginning end at the end of business method)<br />
For related information on different types of advices refer page numbers 104-106.<br />
<strong>Spring</strong> <strong>AOP</strong> supports only method level Aspect oriented programming that means it does not<br />
support Field level Aspect oriented programming.<br />
<strong>Spring</strong> beans borrowed <strong>AOP</strong> fecilities from Alliance company but entire API is inbuilt of<br />
<strong>Spring</strong> software.<br />
Advice: Advice is the code that implements the Aspect.. In general an Aspect defines the<br />
functionality in a more abstract manner. But, Advice that provides a Concrete Implementation<br />
for the Aspect.
Example on AroundAdvice:<br />
Public class MyAdvice implements MethodInterceptor<br />
{<br />
Public Object invoke(MethodInvocation i1) throws Exception<br />
{<br />
-------<br />
-------- //executes at method beginning of the Method<br />
--------- //executes business method<br />
I1.process() //executes at the end of the business smethod.<br />
----------------<br />
--------------<br />
Creating Advices in <strong>Spring</strong>:<br />
There are four types of Advices available in <strong>Spring</strong> those are<br />
1. Before Advice<br />
2. After Advice<br />
3. Throws Advice<br />
4. Around Advice<br />
1. BeforeAdvice: Before Advice is used to intercept before the method execution starts. In<br />
<strong>AOP</strong>, Before Advice is represented in the form of<br />
org.springframeword.aop.BeforeAdvice. For example a sytem makes security checks<br />
before allowing then to accesing resources. In such a case, we can have a Before Advice<br />
that contains code which consider the following piece of code.<br />
EX:<br />
Public class Authentication implements MethodBeforeAdvice // Here MethodBefore Advice<br />
is manadatory to make the Java class BeforeAdvice<br />
{<br />
Public void beforeMethod method, Object[] args, Object target) throws Throwable<br />
{
place the Authentication logic here.<br />
}<br />
}<br />
Above class extends BeforeAdvice, there by telling before method will be called before the<br />
execution of the method call. Note that the java.lang.reflect.Method method object represents<br />
target method to be invoked, Object[] args refers to the various arguments that are passed on<br />
the method and target refers to the object which is calling the method.<br />
2. AfterAdvice: AfterAdvice will be useful if some logic has to be executed before running<br />
the control with in a method execution. This advice is represented by the interface<br />
org.springframework.aop.AfterReturningAdvice. For example, it is common in<br />
Application to Delete the Session data and the various information pertaining to a user,<br />
after he has logged out from the Application. These are ideal candidates for After Advice.<br />
EX: CleanUpOperatoin.java<br />
Public class CleanUpoperation implements AfterReturningAdvice<br />
{<br />
Public void afterReturningObject returnValue, Method method, Object[] arg, Object target<br />
throws Throwable<br />
//Here afterReturning method re[resents the retun value given by the business method.<br />
{<br />
//clean up session and user information<br />
}<br />
}<br />
Note that, afterReturning) method will be called once the method returns normal execution.<br />
If some exception happedns in the method execution the afterReturning method will never be<br />
called.<br />
3. Throws Advice: //represents the exception raised in the business method.<br />
When some kind of exception happens during the execution of a method, then to handle the<br />
exception properly, Throws Advice can be used through the means of<br />
org.springframework.aop.ThrowsAdvice. Note that this interface is a marker interface.
EX:<br />
Public void afterThrowingException ex<br />
{<br />
Public void afterThrowingMethod method, Object[] args, Object target, Exception exception<br />
//here afterThrowing method represents the exception raised in the business method.<br />
AroundAdvice: The Advice is very different from the other types of the advices because of<br />
the fact that, the Advice provides finer control whether the target method has to be called or<br />
not. Considering the above advices, the return type of the method signature is always<br />
meaning that, the Advice that can’t change the return arguments off the brand new object of<br />
other type if needed.<br />
Consider the following code<br />
Public void relateObject o, Object o<br />
{<br />
O1.establishRelationo2);<br />
}<br />
Assume that we have a method that provides an Association llink between two objects, but<br />
before that, we have to ensure the type fo objects being passed must conform to a standard,<br />
by implementing some interfaces. We can have this arguments check in the Around Advice<br />
rather that having it in the actual method implementation. The Around Advice is represented<br />
by org.aopalliance.intercept.MethodInterceptor.<br />
ValidateArguments.java<br />
Public class ValidateArguments implements MEthodInterceptor<br />
{<br />
Public Object invoke(MethodInvocation invocation throws Throwable<br />
{<br />
Object arguments[]=invocation.getArguments<br />
Ifarguments[]instance of Parent)&&arguments[] instanceof Child))
{<br />
Object returnValue=invocation.proceed;<br />
Return returnValue;<br />
}<br />
Throw new Exception”Arguments are of wrong yet”;<br />
}<br />
}<br />
In the above code, the valiudation happens the arguments to check whether they implement<br />
the right interface. It is important to make a call to MethodInvocation.proceed, if we happy<br />
with the arguments validation, else the target method will never gets invoked.<br />
Note: Based on the type of the advice we choose the JointPoint positions in the Business method<br />
will be decided automatically.<br />
To apply advices they are<br />
i. NamedMatchMethodPointAdvisor: applies the advices based on the business method<br />
names.<br />
ii. RegexMethodPointAdvisor: Applies the advices based on the given regular<br />
expression symbol<br />
For related information on Pointcut advisors refer page numbers 107 to 109.<br />
Procedure to develop <strong>Spring</strong> <strong>AOP</strong> style application with Advices:<br />
1. Develop <strong>Spring</strong> interface having declaration of business smethods.<br />
2. Develop <strong>Spring</strong> Bean class having implementation of business methods with business<br />
logic.<br />
3. Develop Advice class having Middleware service logic.<br />
4. Develop the <strong>Spring</strong> configuration file to configure <strong>Spring</strong> Bean.<br />
a) Cfg file.<br />
b) Cfg Advice class
c) Cfg Advisor (NamedMatch/Regurlar expression specifying class bean id and business<br />
method)<br />
d) Cfg org.springframework.ProxyFactoryBean specifying target object id, Advisor Bean.<br />
Note: The ProxyFactoryBean class converts given <strong>Spring</strong> Bean class object to Proxy object and<br />
returns that object.<br />
Note: When business methods are called on Proxy object the business logic will be executed<br />
along with Advices.<br />
5. Develop the Client Application to getProxy object and call business methods on the<br />
Object.<br />
Example Application on <strong>Spring</strong> <strong>AOP</strong> having the user defined advice.<br />
Develop the following resources in the Application.<br />
1. . MyAdviceBefore Advice)<br />
2. MyAdvice<strong>Spring</strong> interface<br />
3. DemoBean.java(<strong>Spring</strong>Bean class<br />
4. ClientApp(ClientApp)<br />
5. <strong>Spring</strong>.cfg.xml<strong>Spring</strong> configuration file<br />
For complete program please refer E:\Frameworks\<strong>Spring</strong>\<strong>Spring</strong> <strong>AOP</strong>\NetBeans 7.1<br />
projects\RegexpMethodPointcutAdvisor(MethodBeforeAdvice)<br />
For more related examples on Around Advice based Sprig <strong>AOP</strong> refer following folders.<br />
For demonstrating the behavior of Around Advice<br />
For complete program please refer E:\Frameworks\<strong>Spring</strong>\<strong>Spring</strong> <strong>AOP</strong>\NetBeans 7.1<br />
projects\NamedMatchMethodPointcutAdvisor(MethodBeforeAdvice) folder<br />
For How to Change the retrun value<br />
For Complete program please refer E:\Frameworks\<strong>Spring</strong>\<strong>Spring</strong> <strong>AOP</strong>\NetBeans 7.1<br />
projects\BaseOnReturnValue folder<br />
Modifying Buiness method argument values<br />
For complete program please refer E:\Frameworks\<strong>Spring</strong>\<strong>Spring</strong> <strong>AOP</strong>\NetBeans 7.1<br />
projects\ModifyingBusinessMethodArgsBaseOnReturnValue folder