Pages Navigation Menu

Coding is much easier than you think

Spring AOP – Around Advice

Spring AOP – Around Advice

 


 

This can be mentioned as a collation of the before mentioned advices. It does functionality of before, after and throws.

 
** UPDATE: Spring Complete tutorial now available here.
 

Step 1 :

 
No major changes in the Iterator and IteratorImpl classes.

File : Iterator.java
 

package com.simpleCodeStuffs.aop;

public interface Iterator {

void goPrevious();
void goNext();
}

 
File : IteratorImpl.java
 

package com.simpleCodeStuffs.aop;
public class IteratorImpl implements Iterator{

String flowName;

public void goNext() {
System.out.println("goNext Method Called on flow - "+flowName);
}
public void goPrevious(){
System.out.println("goPrevious Method called on flow - "+flowName);
}
public String getFlowName() {
return flowName;
}
public void setFlowName(String flowName) {
this.flowName = flowName;
}
}

 

Step 2 :

 
Create a new class AopExampleAroundAdvice
File : AopExampleAroundAdvice.java
 

package com.simpleCodeStuffs.aop;



import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AopExampleAroundAdvice implements MethodInterceptor{

@Override
public Object invoke(MethodInvocation method) throws Throwable {
System.out.println("Before invocation of method : "+method.getMethod());
method.proceed();
System.out.println("After execution of method : "+method.getMethod());
return null;
}
}

 

Step 3 :

Changes in the aopBeans.xml. Similar to previous tutorials,

  • change the value in the list of €œinterceptorNames
  • Define the advisor (advice and pattern)
  • Define the advice (class reference)

 
File : aopBeans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Bean Classes -->
    <bean id="beanTarget" class="com.simpleCodeStuffs.aop.IteratorImpl" >
    <property name="flowName" value="existing code"/>
    </bean>
    
      <!-- Bean configuration -->
    <bean id="businesslogicbean"
        class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>com.simpleCodeStuffs.aop.Iterator</value>
        </property>
        <property name="target">
            <ref local="beanTarget" />
        </property>
        <property name="interceptorNames">
            <list>
                <value>theAroundAdvisor</value>
                
            </list>
        </property>
    </bean>
    
    <bean id="theAroundAdvisor"
      class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="advice">
         <ref local="theAroundAdvice"/>
      </property>
      <property name="pattern">
         <value>.*</value>
      </property>
   </bean>
    <bean id="theAroundAdvice"
      class="com.simpleCodeStuffs.aop.AopExampleAroundAdvice"/>
   </beans> 

 

Step 4 :

No major change in the Main class. Simply place a call to the methods
 
File : SpringAopMain.java
 

package com.simpleCodeStuffs.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopMain {
public static void main(String[] args) {
// Read the configuration file
ApplicationContext context =
        new ClassPathXmlApplicationContext("aopBeans.xml");
// Instantiate an object
Iterator IteratorInterface = (Iterator) context.getBean("businesslogicbean");

// Execute the public method of the bean
IteratorInterface.goNext();
IteratorInterface.goPrevious();
}
}

 

Step 5 :

 
Run the main method. The output is as follows
 
springAroundAdviceOutput
 

Step 6 :

 
Lets do a simple modification and check how AroundAdvice works with exceptions thrown.

 

Insert an exception in the goNext() method of IteratorImpl class to check the working. No change is required in Iterator interface

 

File : Iterator.java
 

package com.simpleCodeStuffs.aop;

public class IteratorImpl implements Iterator{
String flowName;
public void goNext() {
System.out.println("goNext Method Called on flow - "+flowName);
throw new RuntimeException();
}

public void goPrevious(){
System.out.println("goPrevious Method called on flow - "+flowName);
}

public String getFlowName() {
return flowName;
}
public void setFlowName(String flowName) {
this.flowName = flowName;
}
}

 

Simply adding a try-catch block in AopExampleAroundAdvice class to handle the exception,

 

File : AopExampleAroundAdvice.java
 

package com.simpleCodeStuffs.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AopExampleAroundAdvice implements MethodInterceptor{

@Override
public Object invoke(MethodInvocation method) throws Throwable {
System.out.println("Before invocation of method : "+method.getMethod());
try{
method.proceed();
} catch(RuntimeException e){
System.out.println("Exception -"+e +" caught on method "+method.getMethod());
}
System.out.println("After execution of method : "+method.getMethod());
return null;
}
}

 

No other change required. The output is as follows:

 
springAroundAdviceModifiedOutput
 


 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.