码迷,mamicode.com
首页 > 其他好文 > 详细

使用BeanNameAutoProxyCreator实现方法日志代理

时间:2015-07-03 00:32:35      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

使用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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!