百度加本身琢磨,如下亲测有效,因此写下来记录,也方便本身回顾浏览加深印象之类,有什么问题能够评论一块儿解决,不完整之处也请大佬指正,一块儿进步哈哈
(1)首先配置文件:java
<!-- 声明自动为spring容器中配置@aspectj切面的bean建立代理 ,织入切面 --> <aop:aspectj-autoproxy /> <!-- 开启注解扫描 --> <context:component-scan base-package="com.ky.zhjd.**"/> <!-- 为true说明代理基于类被建立(默认false,基于接口被建立) --> <aop:config proxy-target-class="true"></aop:config>
(2)建立一个自定义注解类spring
注意建立时选Annotation,类名我叫ArchivesLog(日志档案的意思)。数据库
ArchivesLog.java内容:json
package com.ky.zhjd.common.log; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * * 自定义注解类 * @author ddz * */ @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ArchivesLog { /** 要执行的操做类型好比:添加操做 **/ public String operationType() default ""; /** 要执行的操做名称好比:添加一条用户数据 **/ public String operationName() default ""; }
(3)新建一个切面类,我叫LogAspect.javaapp
package com.ky.zhjd.common.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; /** * 切面类 * * @author ddz * */ @Aspect @Component("logAspect") public class LogAspect { private static final Logger log = LoggerFactory.getLogger(LogAspect.class); // 配置织入点 @Pointcut("@annotation(ArchivesLog)") 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 { // 得到注解 ArchivesLog controllerLog = getAnnotationLog(joinPoint); System.out.println("---------------自定义注解:" + controllerLog); if (controllerLog == null) { return; } // 得到方法名称 String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); String type = controllerLog.operationType(); String name = controllerLog.operationName(); // 打印日志 这里能够进行插入数据库操做 log.info(">>>>>>>>>>>>>操做类型:", type); log.info(">>>>>>>>>>>>>操做名称:", name); log.info(">>>>>>>>>>>>>类名:", className); log.info(">>>>>>>>>>>>>方法名:", methodName); } catch (Exception exp) { // 记录本地异常日志 log.error("==前置通知异常=="); log.error("异常信息:", exp.getMessage()); exp.printStackTrace(); } } /** * 是否存在注解,若是存在就获取 */ private static ArchivesLog getAnnotationLog(JoinPoint joinPoint) throws Exception { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) { // 拿到自定义注解中的信息 return method.getAnnotation(ArchivesLog.class); } return null; } }
(4)在方法上使用注解 , 而后调用该方法学习
@ArchivesLog(operationType="查询操做",operationName="查询一条用户详情") @RequestMapping(value = "/findByid", produces={"application/json;charset=UTF-8"}) public @ResponseBody BaseResult<Object> findByid(String id) { String s="11"; BaseResult<Object> r=userService.findById(s); System.out.println(r+">>>>>>>>>>"); return r; }
ok 上效果:spa
有什么不完善的地方欢迎指出,一块儿学习3d