posts - 10,comments - 4,trackbacks - 0

LogAroundAdvice 通知
package net.blogjava.dodoma.spring.aop;

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

public class LogAroundAdvice implements MethodInterceptor {
 protected static final Log log = LogFactory.getLog(LogAroundAdvice.class);

 public Object invoke(MethodInvocation arg) throws Throwable {
  // 调用目标对象之前
  log.info("before the target object");
  Object val=arg.proceed();
  //调用目标对象之后
  log.info("the arg is "+arg);
  log.info("after the target object");
  return val;
 }

}

测试方法

package net.blogjava.dodoma.spring.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class HelloTest {
 protected static final Log log = LogFactory.getLog(HelloTest.class);

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  Resource rs = new ClassPathResource("beans.xml");
  BeanFactory bf = new XmlBeanFactory(rs);

  HelloI h = (HelloI) bf.getBean("theBean");
  log.info("starting...");
  try {
   log.info(h.sayHello("ma", "bin"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  log.info("end...");
  
  
  ProxyFactory factory=new ProxyFactory();
  factory.addAdvice(new LogAroundAdvice ());
  factory.setTarget(new Hello("hello"));
  try{
  HelloI hi=(HelloI)factory.getProxy();
  hi.sayHello("ma","bin");}
  catch(Exception e){e.printStackTrace();}
 }

}

posted on 2006-03-28 12:52 dodoma 阅读(273) 评论(1)  编辑  收藏 所属分类: spring

FeedBack:
# re: spring之aop:LogAroundAdvice
2006-03-28 13:02 | o0水易今心
啊哦!

沙发耶

呵呵

继续装点你的家哦!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: