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 閱讀(281)
評論(1) 編輯 收藏 所屬分類:
spring