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