使用BeanNameAutoProxyCreator实现方法执行前和执行后的日志记录
首先实现方法执行前和执行后的日志记录
package com.andeveloper.springdemo.aop;import java.lang.reflect.Method;import org.apache.log4j.Logger;import org.springframework.aop.AfterReturningAdvice;public class LogMethodAfterReturningAdvice implements AfterReturningAdvice { private static Logger logger = Logger.getLogger(LogMethodAfterReturningAdvice.class); @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { logger.info(method.getName() + "执行结果为" + returnValue); }}
package com.andeveloper.springdemo.aop;import java.lang.reflect.Method;import org.apache.log4j.Logger;import org.springframework.aop.MethodBeforeAdvice;public class LogMethodBeforeAdvice implements MethodBeforeAdvice { private static Logger logger = Logger.getLogger(LogMethodBeforeAdvice.class); @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { String args = ""; if(arg1 != null) { for(int i = 0; i < arg1.length; ++i) { if(i != arg1.length - 1) { args += arg1[i] + ","; } else { args += arg1[i]; } } } logger.info(arg0.getName() + "方法开始执行,参数为[" + args + "]"); }}
配置BeanNameAutoProxyCreator代理,实现所有以Controller,Service,Dao结尾的方法自动代理
*Controller *Service *Dao
logMethodBefore logAfterReturning