通过Spring AOP和Mybatis拦截器去统计方法耗时


Costs

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Costs {

	private static Logger LOGGER = LoggerFactory.getLogger(Costs.class);

	public static Object execute(String methodName, Callback callback) throws Throwable {
		LOGGER.info("---------开始{}方法---------", methodName);
		long fm = System.currentTimeMillis();
		try {
			return callback.call();
		} catch (Throwable e) {
			throw e;
		} finally {
			long to = System.currentTimeMillis();
			LOGGER.info("---------结束{}方法,耗时[{}]s---------", methodName, (to - fm) / 1000.0);
		}
	}


	public interface Callback {

		Object call() throws Throwable;
	}
}


SpringCost

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SpringCost {


	@Pointcut("execution(public * com.paic.icore.css.rule.console.mapper.*.*(..))")
	public void log() {
	}

	@Around("log()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		Object[] args = point.getArgs();
		String methosName = point.getSignature().toShortString();
		return Costs.execute(methosName, new Costs.Callback() {

			@Override
			public Object call() throws Throwable {
				return point.proceed(args);
			}
		});
	}
}


MyBatisConfig

import java.lang.reflect.Method;
import java.sql.Statement;
import java.util.Properties;

import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisCost {

	@Bean
	public StatementInterceptor statementInterceptor() {
		StatementInterceptor statementInterceptor = new StatementInterceptor();
		return statementInterceptor;
	}

	@Bean
	public ParameterInterceptor parameterInterceptor() {
		return new ParameterInterceptor();
	}

	@Bean
	public ResultSetInterceptor resultSetInterceptor() {
		return new ResultSetInterceptor();
	}

	@Intercepts({ @Signature(type = StatementHandler.class, method = "query", args = { Statement.class,
			ResultHandler.class }) })
	private class StatementInterceptor implements Interceptor {

		@Override
		public Object intercept(Invocation invocation) throws Throwable {
			String methodName = getShortMethodName(invocation);
			return Costs.execute(methodName, new Costs.Callback() {

				@Override
				public Object call() throws Throwable {
					return invocation.proceed();
				}
			});
		}

		private String getShortMethodName(Invocation invocation) {
			Method method = invocation.getMethod();
			String className = method.getDeclaringClass().getSimpleName();
			return className + "." + method.getName() + "()";
		}

		@Override
		public Object plugin(Object target) {
			return Plugin.wrap(target, this);
		}

		@Override
		public void setProperties(Properties properties) {
		}
	}


	@Intercepts({ @Signature(type = StatementHandler.class, method = "parameterize", args = { Statement.class }) })
	private class ParameterInterceptor extends StatementInterceptor {
	}

	@Intercepts({ @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = { Statement.class }) })
	private class ResultSetInterceptor extends StatementInterceptor {
	}
}

发表评论

0 评论
  • 最新评论
  • 按热度排序