AOP Simple example

There is a simple Spring AOP Style example that took me some time to finish it. It's so easy and enjoyable. Hope it would do some help to you.

代码:


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * User: xxxxxxx
 * Date: Dec 17, 2003
 * Time: 7:54:56 PM
 * all rights reserved by utstarcom
 */
public class MyInterceptor implements MethodInterceptor {
    private final Log logger=LogFactory.getLog(MyInterceptor.class);
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        logger.info("Beginning method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
        for(int i=0;i            logger.info(methodInvocation.getArgument(i));
        }
        long startTime = System.currentTimeMillis();
        try {
            Object retVal = methodInvocation.proceed();
            return retVal;
        } finally {
            logger.info("Ending method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
            logger.info("Method invocation time: " + (System.currentTimeMillis() - startTime) + " msecs.");
        }
    }
}



代码:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * User: xxxxxx
 * Date: Dec 17, 2003
 * Time: 8:07:41 PM
 * all rights reserved by utstarcom
 */
public class HelloWorldImpl implements HelloWorld{
    private final static Log logger = LogFactory.getLog(HelloWorldImpl.class);

    public void sayHello(int age,String name){
        logger.info("age:"+age+" name:"+name);
    }
}




代码:

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

import java.io.InputStream;
import java.io.FileInputStream;

/**
 * User: xxxxx
 * Date: Dec 17, 2003
 * Time: 8:00:38 PM
 * all rights reserved by utstarcom
 */
public class StudyAOP {
    public static void main(String[] args){
        //try to initialize the BeanFactory
        BeanFactory factory = null;
        InputStream in=null;
        try{
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.xml");
            factory=new XmlBeanFactory(in);
            HelloWorld greet=(HelloWorld)factory.getBean("greeting");
            greet.sayHello(10,"zhonglin");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{in.close();}catch(Exception e){}
        }

    }
}

 

代码:







   
   

   
       
            HelloWorld
       

       
           
                myInterceptor
                helloworld
           

       

   




代码:

/**
 * User: xxxxx
 * Date: Dec 17, 2003
 * Time: 8:17:19 PM
 * all rights reserved by utstarcom
 */
public interface HelloWorld {
    void sayHello(int age,String name);
}

 

         come from http://spring.jactiongroup.net/viewtopic.php?t=144

你可能感兴趣的:(AOP)