用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
james_sc的公告
<p> <a href="http://member.bcentral.com/cgi-bin/fc/fastcounter-login?3254808" target="_top"> <img border="0" src="http://fastcounter.bcentral.com/fastcounter?3254808+6509623"></a> </p> <p> <a href="http://free.txwww.com/guestbook/index.asp?user=james_sc" target="_blank" class="leftHerf"><b>过客留言</b></a> </p>
文章分类
James' Web Link
Banq's Jdon
Chedong's Blog
James' Studio
Jason's Blog
Mars Studio
Martin Fowler's Web
Yangge's Art
Open Source's Web
Apache Software
PicoContainer
SourceForge
存档

原创  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

发表于 @ 2004年07月08日 00:24:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:Inversion of Control (控制倒置) | 新一篇:阿根廷是一种病,一种美妙的病

  • 发表评论
  • 评论内容:
  •  
Copyright © james_sc
Powered by CSDN Blog