java中的logafter_spring之aop:LogAfterAdvice

接口和实现类见LogBeforeAdvice的例子

package net.blogjava.dodoma.spring.aop;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.aop.AfterReturningAdvice;

public class LogAfterAdvice implements AfterReturningAdvice {

protected static final Log log = LogFactory.getLog(LogAfterAdvice.class);

public void afterReturning(Object returnVal, Method m, Object[] args,

Object target) throws Throwable {

// TODO Auto-generated method stub

log.info("in the class "+this.getClass().getName()+"'s method afterReturning()");

log.info("the target class is:" + target.getClass().getName());

log.info("the target method is:" + m.getName());

for (int i = 0; i < args.length; i++) {

log.info("the method's args is:" + args[i]);

}

log.info("the returnValue is "+returnVal);

//测试,如果返回装备发生了异常.将如何处理程序流程

//throw new Exception("返回装备发生异常");

}

}

测试代码

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 LogAfterAdvice());

factory.setTarget(new Hello("hello"));

try{

HelloI hi=(HelloI)factory.getProxy();

hi.sayHello("ma","bin");}

catch(Exception e){e.printStackTrace();}

}

}

在beans.xml中加入如下代码

theLogAfterAdvice

切入点可省略,如需要的话

.*

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值