标签:
使用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结尾的方法自动代理
<bean id="logMethodBefore" class="com.andeveloper.springdemo.aop.LogMethodBeforeAdvice" /> <bean id="logAfterReturning" class="com.andeveloper.springdemo.aop.LogMethodAfterReturningAdvice" /> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Controller</value> <value>*Service</value> <value>*Dao</value> </list> </property> <property name="interceptorNames"> <list> <value>logMethodBefore</value> <value>logAfterReturning</value> </list> </property> </bean>
使用BeanNameAutoProxyCreator实现方法日志代理
标签:
原文地址:http://my.oschina.net/bankofchina/blog/473821