Spring AOP 实现自定义注解+日志管理

package cc.cloud.log;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LogRecord {
	String title() default"";
	String action() default"";
}
package cc.cloud.log;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component("logAspect")
public class LogAspect {
	
	private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
	 
    // 配置织入点
    @Pointcut("@annotation(cc.cloud.log.LogRecord)")
    public void logPointCut() {
    }
 
    /**
     * 前置通知 用于拦截操作,在方法返回后执行
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "logPointCut()")
    public void doBefore(JoinPoint joinPoint) {
	handleLog(joinPoint, null);
    }
 
    /**
     * 拦截异常操作,有异常时执行
     * 
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfter(JoinPoint joinPoint, Exception e) {
	handleLog(joinPoint, e);
    }
 
    private void handleLog(JoinPoint joinPoint, Exception e) {
	try {
	    // 获得注解
	    LogRecord controllerLog = getAnnotationLog(joinPoint);
	    if (controllerLog == null) {
		return;
	    }
	    // 获得方法名称
	    String className = joinPoint.getTarget().getClass().getName();
	    String methodName = joinPoint.getSignature().getName();
	    String action = controllerLog.action();
	    String title = controllerLog.title();
	    //打印日志,如有需要还可以存入数据库
	    log.info("模块名称:{}",title);
	    log.info("操作名称:{}",action);
	    log.info("类名:{}",className);
	    log.info("方法名:{}",methodName);
	} catch (Exception exp) {
	    // 记录本地异常日志
	    log.error("==前置通知异常==");
	    log.error("异常信息:{}", exp.getMessage());
	    exp.printStackTrace();
	}
    }
 
    /**
     * 是否存在注解,如果存在就获取
     */
    private static LogRecord getAnnotationLog(JoinPoint joinPoint) throws Exception {
	Signature signature = joinPoint.getSignature();
	MethodSignature methodSignature = (MethodSignature) signature;
	Method method = methodSignature.getMethod();
	if (method != null) {
	    return method.getAnnotation(LogRecord.class);
	}
	return null;
    }
}
    @RequestMapping("examList")
    @ResponseBody
    @LogRecord(title="考试列表",action="获取考试列表")
    public ExamsVO examList(HttpServletRequest request, String keyword, String termId) {
        .
        .
        .
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值