Java tutorial
/* * Copyright 2012-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package duokan.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Aspect public class ServiceMonitor { private static final Logger logger = LoggerFactory.getLogger(ServiceMonitor.class); @AfterReturning("execution(* duokan.service.*Service.*(..))") public void logServiceAccess(JoinPoint joinPoint) { logger.error("Completed: " + joinPoint); } @Around("execution(* duokan.service.*Service.*(..))") public Object around(ProceedingJoinPoint point) { long start = System.currentTimeMillis(); Object result = null; try { result = point.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } /** http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html */ logger.error("MethodName:{};;Arguments:{};;Result:{};;Calculated in [***[{}]***][msec]s", MethodSignature.class.cast(point.getSignature()).getMethod().getName(), point.getArgs(), result, System.currentTimeMillis() - start); return result; } }