使用Spring AOP中MethodInterceptor记录日志

    应用中可使用AOP的特性来记录各层(采用分层设计的状况下)的日志:java

1,实现MethodInterceptorapache

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

public class ServiceMethodInterceptor implements MethodInterceptor{
     private final Log LOG = LogFactory.getLog(ServiceMethodInterceptor.class);

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object object = null;
        LOG.info("before service....");
        object = invocation.proceed();
        LOG.info("after service....");
        return object;
    }
    
}

2,配置MethodInterceptoride

<bean id="serviceMethodInterceptor" class="*.ServiceMethodInterceptor"/>
.........
相关文章
相关标签/搜索