Java tutorial
/** * Copyright (c) 2011-2020, hubin (jobob@qq.com). * * 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 com.wshsoft.springmvc.common.aop; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.wshsoft.springmvc.common.annotations.Log; /** * <p> * ?? * </p> * * @author Carry xie * @Date 2016-05-09 */ @Aspect @Component public class LogAspect { private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); /** * */ @Autowired private LogPoint logPoint; /** * ?? * * @param joinPoint * * @return * @throws Throwable * */ @Around(value = "@annotation(com.wshsoft.springmvc.common.annotations.Log)") public Object saveLog(ProceedingJoinPoint joinPoint) throws Throwable { /** * ?Log */ String methodName = joinPoint.getSignature().getName(); Method method = currentMethod(joinPoint, methodName); Log log = method.getAnnotation(Log.class); /** * */ if (log != null) { logPoint.saveLog(joinPoint, methodName, log.value(), null); } /** * */ return (joinPoint).proceed(); } /** * ?? * * @param joinPoint * * @param methodName * ?? * @return */ private Method currentMethod(ProceedingJoinPoint joinPoint, String methodName) { /** * ??? */ Method[] methods = joinPoint.getTarget().getClass().getMethods(); Method resultMethod = null; for (Method method : methods) { if (method.getName().equals(methodName)) { resultMethod = method; break; } } return resultMethod; } @Pointcut("execution(* com.wshsoft.springmvc.controller..*.*(..)) " + "|| execution(* com.wshsoft.springmvc.service..*.*(..))") public void exceptionLogCall() { } @AfterThrowing(pointcut = "exceptionLogCall()", throwing = "e") public void doThrowing(JoinPoint joinPoint, Exception e) { try { logPoint.saveLog(joinPoint, joinPoint.getSignature().getName(), "", e); } catch (Exception exp) { logger.error("?:{}", exp.getMessage()); } } public LogPoint getLogPoint() { return logPoint; } public void setLogPoint(LogPoint logPoint) { this.logPoint = logPoint; } }