使用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"/>
.........
相關文章
相關標籤/搜索