Spring AOP复杂的日志记录(自定义注解)
做项目中,业务逻辑要求只要对数据库数据进行改动的都需要记录日志(增删改),记录的内容有操作者、操作的表名及表名称、具体的操作,以及操作对应的数据。
首先想到的就是Spring 的AOP功能。可是经过一番了解过后,发现一般的日志记录,只能记录一些简单的操作,例如表名、表名称等记录不到。
于是想到了自定义注解的方法,把想要记录的内容放在注解中,通过切入点来获取注解参数,就能获取自己想要的数据,记录数据库中。顺着这个思路,在网上查找了一些相关资料,最终实现功能。话不多说,以下就是实现的思路及代码:
第一步
在代码中添加自定义注解,并且定义两个属性,一个是日志的描述(description),还有个是操作表类型(tableType),属性参数可按需求改变。代码如下:
- 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;
- /**
- * ClassName: SystemServiceLog <br/>
- * Function: AOP日志记录,自定义注解 <br/>
- * date: 2016年6月7日 上午9:29:01 <br/>
- * @author lcma
- * @version
- * @since JDK 1.7
- */
- @Target({ElementType.PARAMETER, ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface SystemServiceLog {
- /**
- * 日志描述
- */
- String description() default "";
-
- /**
- * 操作表类型
- */
- int tableType() default 0;
- }
第二步
定义切面类,获取切面参数,保存数据库具体代码如下:
需要注意的是,定义切点的时候,@Pointcut里面是自定义注解的路径
每个切面传递的数据的都不一样,最终决定,获取切面的所有参数,转成json字符串,保存到数据库中。
第三步
在service需要记录日志的地方进行注解,代码如下:
- @SystemServiceLog(description=Constants.ADMIN_SAVE_OPTIONS,tableType=Constants.ADMIM_TABLE_TYPE)
代码图片:

在常量类里面配置自定义注解的参数内容:


第四步
把切面类所在的包路径添加到Spring注解自动扫描路径下,并且启动对@AspectJ注解的支持,代码如下:
- <!-- 启动对@AspectJ注解的支持 -->
- <aop:aspectj-autoproxy proxy-target-class="true" />
- <!-- 自动扫描包路径 -->
- <context:component-scan base-package="com.iflytek.zhbs.common.aoplog" />
- <context:component-scan base-package="com.iflytek.zhbs.service" />
最后数据库记录数据的效果如图:

OK,功能已经实现,初次写博客,写的不好的地方请谅解。
多个注解可以合并成一个,包括自定义注解
spring中有时候一个类上面标记很多注解。
实际上Java注解可以进行继承(也就是把多个注解合并成1个)
比如说SpringMVC的注解
- @RestController
- @RequestMapping("/person")
可以合并为一个
- @PathRestController("/user")
实现是:
- 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;
- import org.springframework.core.annotation.AliasFor;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @RestController
- @RequestMapping
- public @interface PathRestController {
- @AliasFor("path")
- String[] value() default {};
- @AliasFor("value")
- String[] path() default {};
- }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。