WELCOME Abdennour : Software engineer

Apr 5, 2012

AOP Spring with practices(PART5):complete example of AOP without Aspectj


It means we will not use aspectj.
We must therefore create our proxy and our interceptors and manually wrap the target by the interceptor suitable.
Beginning with the Create Interceptors:
1) Execute something before the execution of the method Target:

  1: /*
  2:  * To change this template, choose Tools | Templates
  3:  * and open the template in the editor.
  4:  */
  5: package slm.abdennour.aop.interceptors;

  7: import java.lang.reflect.Method;
  8: import org.springframework.aop.MethodBeforeAdvice;
  9: /**
 10:  * 
 11:  * @author Abdennour toumi
 12:  * @since 28 mars 2012 / 07:40:46
 13:  */
 14: public class AbdennourBeforeMethodExec implements MethodBeforeAdvice {

 16:     public void before(Method method, Object[] os, Object o) throws Throwable {
 17:             System.out.println("Abdennour you said :pay attention,Your Methode "+method.getName()+" will be executed");
 18:  
 19:     }
 20:     
 21: }

2)Execute something after a return of the method Target:



package slm.abdennour.aop.interceptors;



import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/**
 @author Abdennour toumi

 @since 28 mars 2012 / 07:40:55

 */
public class AbdennourAfterMethodReturn implements AfterReturningAdvice {


    public void afterReturning(Object o, Method method, Object[] os, Object o1throws Throwable {

        System.out.println("Abdennour you said :Congratulations,Your Methode "+method.getName()+" is well executed");

    }

}


3)Execute something After Throws(Bad return) of the method Target:


package slm.abdennour.aop.interceptors;
import org.springframework.aop.ThrowsAdvice;
/**

 @author Abdennour toumi

 @since 28 mars 2012 / 07:41:17

 */

public class AbdennourThrowException implements ThrowsAdvice {

     public void afterThrowing(IllegalArgumentException ethrows Throwable {

    System.out.println("Unfortunately. The Method did not pass because "+e.toString());

  }

}

4)Execute something before&after the execution of the method Target:



package slm.abdennour.aop.interceptors;

import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

/**

 @author Abdennour toumi

 @since 28 mars 2012 / 07:41:08

 */

public class AbdennourBothBeforeAfter implements MethodInterceptor {

    String nameOfMethodInvoked;

    public Object invoke(MethodInvocation methodInvocationthrows Throwable {

        nameOfMethodInvoked = methodInvocation.getMethod().getName();

        System.out.println("Abdennour Invoke the method " + nameOfMethodInvoked);

        System.out.println("Args of " + nameOfMethodInvoked + ((methodInvocation.getArguments().length == 1"is " "are "+ Arrays.toString(methodInvocation.getArguments()));

        System.out.println(" (Around)PAy attention the Method   [" + nameOfMethodInvoked + "]will be executed");

        // proceed to original method call

        try {

            Object result = methodInvocation.proceed();

            System.out.println(" (Around)Congra. the Method   [" + nameOfMethodInvoked + "]is executed well");

            return result;

        catch (IllegalArgumentException e) {

            System.out.println(" (Around)Throwing the Method   [" + nameOfMethodInvoked + "]");

            throw e;

        }


    }

}



Target Class:(Which is wrapped by Proxy)
  5: package slm.abdennour.core;
  7: /**
  8:  * 
  9:  * @author Abdennour toumi
 10:  * @since 28 mars 2012 / 07:41:36
 11:  */
 12: public class CustomerService {

 14:     private String name;
 15:     private String url;

 17:     public void setName(String name) {
 18:         this.name = name;
 19:     }

 21:     public void setUrl(String url) {
 22:         this.url = url;
 23:     }

 25:     public void printName() {
 26:         System.out.println("Customer name : " + this.name);
 27:     }

 29:     public void printURL() {
 30:         System.out.println("Customer website : " + this.url);
 31:     }

 33:     public void printThrowException() {
 34:         throw new IllegalArgumentException();
 35:     }
 36: }

Define Proxy ,Target & Interceptors In Configuration Spring Xml :
1)Before Execution :"Spring-Customer.xml"

 <!--TARGET -->
 <bean id="customer" class="slm.abdennour.core.CustomerService" p:name="Abdennour" p:url="http://abdennour-insat.blogspot.com" />
  <!--Interceptor -->
 <bean id="abdennourBeforeMethod" class="slm.abdennour.aop.interceptors.AbdennourBeforeMethodExec" />
  <!-- PROXY -->
 <bean id="proxyCustomer" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
           <property name="interceptorNames" >
                    <list>
                        <value>abdennourBeforeMethod</value>
                    </list>    
           </property>    

 </bean>


2)After Return:"Spring-Customer.xml"

<!--Interceptor -->
 <bean id="afterReturnMethod" class="slm.abdennour.aop.interceptors.AbdennourAfterMethodReturn" />

<!-- PROXY -->


 <bean id="proxyCustomerAfter" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
         <property name="interceptorNames" >
           <list>
                   <value>afterReturnMethod</value>    
           </list>    
         </property>
 </bean>  

3)After Throws:"Spring-Customer.xml"

<!--Interceptor -->
  <bean id="throwMethod" class="slm.abdennour.aop.interceptors.AbdennourThrowException" />
  <!-- PROXY -->
 <bean id="proxyThrowing" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
     <property name="interceptorNames" >
         <list>
            <value>throwMethod</value>
         </list>
     
     </property>   

 </bean>
     


4)Before&After:"cfgForAroundAdvice.xml"
  <bean id="customer" class="slm.abdennour.core.CustomerService" p:name="Omar" p:url="http://abdennour-insat.blogspot.com"  />
  <!--Interceptor -->
  <bean id="aroundBean" class="slm.abdennour.aop.interceptors.AbdennourBothBeforeAfter" />
  <!-- PROXY -->
  <bean id="aroundProxyCustomer" class="org.springframework.aop.framework.ProxyFactoryBean" p:target-ref="customer" >
          <property name="interceptorNames" >
          <list>
          <value>aroundBean</value>    
          </list>    
          </property>
  </bean>

Test The For Interceptors:
import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

 @author Abdennour toumi

 @since 28 mars 2012 / 07:41:29

 */

public class App {



    public static void main(String[] args) {

       // tst1Dummy();

        //tst2MethodBeforeExecution();;

      //  tst3MethodAfterReturn();

       //tst4MethodAfterThrowing();

        tst5Around();


    }

    public static void tst1Dummy(){

        ApplicationContext appContext = new ClassPathXmlApplicationContext(

                new String[]{"Spring-Customer.xml"});



        CustomerService cust = (CustomerServiceappContext.getBean("customer");

        System.out.println("*************************");

        cust.printName();

        System.out.println("*************************");

        cust.printURL();

        System.out.println("*************************");

        try {

            cust.printThrowException();

        catch (Exception e) {

        }

    }

    public static void tst2MethodBeforeExecution(){

        

                ApplicationContext appContext = new ClassPathXmlApplicationContext(

        new String[] { "Spring-Customer.xml" });

 

    CustomerService cust = 

                                (CustomerServiceappContext.getBean("proxyCustomer");

 

    System.out.println("*************************");

    cust.printName();

    System.out.println("*************************");

    cust.printURL();

    System.out.println("*************************");

    try {

      cust.printThrowException();

    catch (Exception e) {

 

    }

 

    }

     public static void tst3MethodAfterReturn(){

        

                ApplicationContext appContext = new ClassPathXmlApplicationContext(

        new String[] { "Spring-Customer.xml" });

 

    CustomerService cust = 

                                (CustomerServiceappContext.getBean("proxyCustomerAfter");

 

    System.out.println("*************************");

    cust.printName();

    System.out.println("*************************");

    cust.printURL();

    System.out.println("*************************");

    try {

      cust.printThrowException();

    catch (Exception e) {

 

    }

 

    }

     public  static void  tst4MethodAfterThrowing(){

          ApplicationContext appContext = new ClassPathXmlApplicationContext(

        new String[] { "Spring-Customer.xml" });

 

    CustomerService cust = 

                                (CustomerServiceappContext.getBean("proxyThrowing");

 

    System.out.println("*************************");

    cust.printName();

    System.out.println("*************************");

    cust.printURL();

    System.out.println("*************************");

    try {

      cust.printThrowException();

    catch (Exception e) {

 

    }

 

     }

     public static void tst5Around(){

          ApplicationContext appContext = new ClassPathXmlApplicationContext(

        new String[] { "cfgForAroundAdvice.xml" });

 

    CustomerService cust = 

                                (CustomerServiceappContext.getBean("aroundProxyCustomer");

 

    System.out.println("*************************");

    cust.printName();

    System.out.println("*************************");

    cust.printURL();

    System.out.println("*************************");

    try {

      cust.printThrowException();

    catch (Exception e) {

 

    }

     }

}

 /* OUTPUT Of tst5(Around)*/


 (Around)PAy attention the Method   [printURL]will be executed

Customer website : http://abdennour-insat.blogspot.com

 (Around)Congra. the Method   [printURL]is executed well

*************************

Abdennour Invoke the method printThrowException

Args of printThrowExceptionare []

 (Around)PAy attention the Method   [printThrowException]will be executed

 (Around)Throwing the Method   [printThrowException]

      */

   






No comments:

Post a Comment