AOP Samples

ApplicationContext.xml

xml version="1.0" encoding="UTF-8"?>

<

beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"


>



<bean id="bModel" class="com.sp.sample.aop.BusinessModel"></bean>

<bean id="beforeAspect" class="com.sp.sample.aop.BeforeAspectAdvicer"></bean>



<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="bModel"></property>

<property name="interceptorNames">

<list>

<value>beforeAspect</value>

</list>

</property>

</bean>



<bean id="afterAspect" class="com.sp.sample.aop.AfterAspectAdvicer"></bean>

<bean id="afterProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="bModel"></property>

<property name="interceptorNames">

<list>

<value>afterAspect</value>

</list>

</property>

</bean>



<bean id="aroundAspect" class="com.sp.sample.aop.AroundAspectAdvicer"></bean>

<bean id="aroundProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="bModel"></property>

<property name="interceptorNames">

<list>

<value>aroundAspect</value>

</list>

</property>

</bean>



 

<bean id="validator" class="com.sp.sample.aop.AspectValidator"></bean>

<bean id="exceptionAspect" class="com.sp.sample.aop.ExceptionAspectAdvicer"></bean>

<bean id="exceptionProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target" ref="validator"></property>

<property name="interceptorNames">

<list>

<value>exceptionAspect</value>

</list>

</property>

</bean>



</

beans>
 
 
AfterAspectAdvicer


/**
 *
 */
package com.sp.sample.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
/**
 * @author PraveenKumar
 *
 */
public class AfterAspectAdvicer implements AfterReturningAdvice {
 @Override
 public void afterReturning(Object arg0, Method arg1, Object[] arg2,
   Object arg3) throws Throwable {
 
  System.out.println("looked up the After");
  System.out.println("Called the After actual process...!");
 
 }
}

AopMainModel1

/**
 *
 */
package com.sp.sample.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
 * @author PraveenKumar
 *
 */
public class AopMainModel1 {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 
  Resource r=new ClassPathResource("applicationContext.xml"); 
      BeanFactory factory=new XmlBeanFactory(r); 
    
      /**
       * This is for Before aspect
       */
      System.out.println("Testing Before Aspect Advicer");
      BusinessModel bModel=factory.getBean("proxy",BusinessModel.class);
      bModel.processBusinesslogic();
      /**
       * This is for after aspect
       */
      System.out.println("Testing After Aspect Advicer");
      BusinessModel AModel=factory.getBean("afterProxy",BusinessModel.class);
      AModel.processBusinesslogic();
     
      System.out.println("Testing both together Aspect Advicer");
      BusinessModel arModel=factory.getBean("aroundProxy",BusinessModel.class);
      arModel.processBusinesslogic();
     
      System.out.println("Testing Exception handlings");
      AspectValidator validator= factory.getBean("exceptionProxy",AspectValidator.class);
      try{
       validator.validate("T");
       validator.validate("F");
      }catch(Exception e){
       e.printStackTrace();
      }
     
 }
}
------------------------------
/**
 *
 */
package com.sp.sample.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
 * @author PraveenKumar
 *
 */
public class AroundAspectAdvicer implements MethodInterceptor {
 @Override
 public Object invoke(MethodInvocation methodInvoke) throws Throwable {
 
  Object objToReturn; 
          System.out.println("Invoking around aspect advicer -- before process"); 
          objToReturn=methodInvoke.proceed(); 
          System.out.println("Invoking around aspect advicer -- after process");
         return objToReturn; 
 
 }
}
-----------------------------
/**
 *
 */
package com.sp.sample.aop;
/**
 * @author PraveenKumar
 *
 */
public class AspectValidator {
 public void validate(String isThrowAdvice) throws Exception{
 
  if(isThrowAdvice.equalsIgnoreCase("T")){
   System.out.println("Yes you are in right track.... All the best... !");
  }else{
   throw new Exception("your input is wrong... Kindly verify it once again...!");
  }
 
 }
}
--------------------------------------
/**
 *
 */
package com.sp.sample.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
 * @author PraveenKumar
 *
 */
public class BeforeAspectAdvicer implements MethodBeforeAdvice {
 @Override
 public void before(Method arg0, Object[] arg1, Object arg2)
   throws Throwable {
 
  System.out.println("looked up the before");
  System.out.println("Called the before actual process...!");
 
 }
}
----------------------------------------------
/**
 *
 */
package com.sp.sample.aop;
/**
 * @author PraveenKumar
 *
 */
public class BusinessModel {
 public void processBusinesslogic(){
  System.out.println("method call from processBusinessLogic ... actual pprocessing method");
 }
}
-------------------------------------------------------
/**
 *
 */
package com.sp.sample.aop;
import org.springframework.aop.ThrowsAdvice;
/**
 * @author PraveenKumar
 *
 */
public class ExceptionAspectAdvicer implements ThrowsAdvice{
 public void afterThrowing(Exception ex){ 
          System.out.println("This is specific to Exception handling ... !"); 
     } 
}
-------------------------------------

Comments

Popular posts from this blog

Spring MVC with Sqlite sample

Struts Tutorial Page