使用AOP+Annotation实现操作日志记录
先创建注解 OperInfo
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface OperInfo {
public String description() default "";//操作说明
}
然后创建AOP的切面类 LogAspect
public class LogAspect {
@Resource
private OperationLogService operService;
public Object doAroundMethodForCtLog(ProceedingJoinPoint pjp)
throws Throwable{
Method method = getMethod(pjp);
if(method != null && method.isAnnotationPresent(OperInfo.class)){
OperInfo annotation = method.getAnnotation(OperInfo.class);
String descpTemp = annotation.description();
String description = executeTemplate(descpTemp, pjp);
HttpServletRequest request = HttpServletUtils.getRequest();
Users user = (Users) request.getSession().getAttribute("users");
OperationLog operation = new OperationLog();
operation.setChangetime(new Date());
operation.setChangeuserid(user.getUsersid());
operation.setOptid(UUIDUtils.getUUID());
operation.setDescription("用户:"+ user.getUsername() +",日志信息:【{"+description+"}】");
operService.insertService(operation);
}
return pjp.proceed();
}
//解析SPEL
private String executeTemplate(String template, ProceedingJoinPoint joinPoint){
LocalVariableTableParameterNameDiscoverer parameterNameDiscovere = new LocalVariableTableParameterNameDiscoverer();
Method method = getMethod(joinPoint);
String[] parameterNames = parameterNameDiscovere.getParameterNames(method);
EvaluationContext context = new StandardEvaluationContext();
ExpressionParser parser = new SpelExpressionParser();
Object[] args = joinPoint.getArgs();
if(args.length == parameterNames.length){
for (int i = 0,len = args.length; i < len; i++) {
context.setVariable(parameterNames[i], args[i]);
}
}
return parser.parseExpression(template, new TemplateParserContext()).getValue(context, String.class);
}
private Method getMethod(ProceedingJoinPoint joinPoint){
Object target = joinPoint.getTarget();
Method method = ((MethodSignature)joinPoint.getSignature()).getMethod();
try {
method = target.getClass().getMethod(method.getName(), method.getParameterTypes());
} catch (Exception e) {
e.printStackTrace();
}
return method;
}
}
spring-mvc.xml配置
<bean class="com.aop.LogAspect" id="logAspect" />
<aop:config expose-proxy="true">
<aop:pointcut expression="execution(* com.service.impl.*Impl.*(..))" id="txPointcut"/>
<aop:aspect ref="logAspect" >
<aop:around method="doAroundMethodForCtLog" pointcut-ref="txPointcut" />
</aop:aspect>
</aop:config>
最后在需要拦截的service上加上注解就行了
@OperInfo(description="新增了名为#{#user.username}的用户")
public int insertUser(Users user){
}
使用AOP+Annotation实现操作日志记录的更多相关文章
- 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...
- 来一手 AOP 注解方式进行日志记录
系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...
- Winform开发框架之权限管理系统改进的经验总结(4)-一行代码实现表操作日志记录
在前面介绍了几篇关于我的权限系统改进的一些经验总结,本篇继续这一系列主体,介绍如何一行代码实现重要表的操作日志记录.我们知道,在很多业务系统里面,数据是很敏感的,特别对于一些增加.修改.删除等关键的操 ...
- 基于SqlSugar的开发框架循序渐进介绍(8)-- 在基类函数封装实现用户操作日志记录
在我们对数据进行重要修改调整的时候,往往需要跟踪记录好用户操作日志.一般来说,如对重要表记录的插入.修改.删除都需要记录下来,由于用户操作日志会带来一定的额外消耗,因此我们通过配置的方式来决定记录那些 ...
- .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录
结合前面封装的ExceptionLess,接下来使用 AspectCore 实现AOP日志处理 nuget导入AspectCore.Core .AspectCore.Extensions.Depend ...
- Spring Boot分布式系统实践【基础模块构建3.3】注解轻松实现操作日志记录
日志注解 前言 spring切面的编程,spring中事物处理.日志记录常常与pointcut相结合 * * Pointcut 是指那些方法需要被执行"AOP",是由"P ...
- springAOP实现操作日志记录,并记录请求参数与编辑前后字段的具体改变
本文为博主原创,未经允许不得转载: 在项目开发已经完成多半的情况下,需要开发进行操作日志功能的开发,由于操作的重要性,需要记录下操作前的参数和请求时的参数, 在网上找了很多,没找到可行的方法.由于操作 ...
- springboot—spring aop 实现系统操作日志记录存储到数据库
原文:https://www.jianshu.com/p/d0bbdf1974bd 采用方案: 使用spring 的 aop 技术切到自定义注解上,针对不同注解标志进行参数解析,记录日志 缺点是要针对 ...
- 利用AOP与ToStringBuilder简化日志记录
刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...
随机推荐
- [每日电路图] 9、介绍一种低价简单的4.2V锂电池充电及稳压(LDO)电路——RT9193,SD8017
消费类电子电路图设计中往往会涉及到4.2V锂离子电池充电电路.及4.2V转3.3V的LDO稳压电路,这里分享一种简单.便宜的设计方案: RT9193 300mA,Ultra-Low Noise,Ult ...
- 判断手机端用户打开页面时是android还是ios,并将判断结果通过ajax返回给url接口,传递回去
首先判断页面是android还是ios,然后利用ajax将结果通过接口url返回回去,记录到log日志中,以统计android和ios用户访问该页面的数量(数据统计) <script type= ...
- 今天不谈技术,说说一些常用的软件~By 逆天
前端工具:http://www.cnblogs.com/dunitian/p/5640147.html 在线办公: http://word.baidu.com/welcome.html http ...
- Sql Server系列:游标
1. 游标简介 游标是一种处理数据的方法,主要用于存储过程.触发器和Transact-SQL脚本中.SELECT语句返回的是一个结果集,游标能够从包含多条数据记录的结果集中每次提取一条记录. 游标的特 ...
- IOS开发资料汇总
1 IOS账号注册.程序发布流程 1)http://jamesli.cn/blog/?p=955 2)http://jamesli.cn/blog/?p=966 3)http://jamesli.cn ...
- JAVA服务器搭建之问题总结
负责维护公司产品的web服务器搭建与维护,最近遇到一下状况,今天在这里简单总结一下,希望对于刚刚一些刚入行的小伙伴有所帮助,避免再走弯路. 第一点:Tomcat内存设置: 一.常见的Java内存溢出有 ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
- 3、Redis 基础
Redis的五大数据类型 String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value.string类型是二进制安全的.意 ...
- EntityFramework 7 Join Count LongCount 奇怪问题(已修复)
问题说明: 博客问题纪录 Use EF7, Linq Join Count is error EF7 Code Commit EF7 版本(注意 rc): 旧版本:"EntityFramew ...
- 在Excel表里面插入背景图
工作中我们会经常用到MS Excel,通常我们打开MS Excel,里面的工作表都是空白单调的背景.当然了,MS Excel可以在工作簿里面插入背景图片.那么问题来了,如果你没有安装Microsoft ...