James Studio

Java 、Open Source And Design Patterns

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. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

代码:


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

阅读更多
个人分类: AOP
想对作者说点什么? 我来说一句

ctp stock file

2015年04月25日 418B 下载

Sample of Android SysConfig

2015年11月09日 57KB 下载

SystemC TLM example by simple bus

2011年01月11日 107KB 下载

vs2008 example

2009年01月01日 899B 下载

Simple VB .NET OPC Example

2012年09月08日 194KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭