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: <a href="mailto:xxxxx@utstar.com">xxxxxxx</a>
 * 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<methodInvocation.getArgumentCount();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: <a href="mailto:xxxxx@utstar.com">xxxxxx</a>
 * 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: <a href="mailto:xxxxxx@utstar.com">xxxxx</a>
 * 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){}
        }

    }
}

 

代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>

    <bean id="helloworld" class="HelloWorldImpl"/>
    <bean id="myInterceptor" class="MyInterceptor"/>

    <bean id="greeting" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>HelloWorld</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>myInterceptor</value>
                <value>helloworld</value>
            </list>
        </property>
    </bean>
</beans>




代码:

/**
 * User: <a href="mailto:xxxxxx@utstar.com">xxxxx</a>
 * 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值