springAOP记录用户操作日志
项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用springAOP来完成比较合适。
注解工具类:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogAnnotation {
String operateModelNm() default "";
String operateFuncNm() default "";
String operateDescribe() default "";
}
切面类:
@Aspect
public class MyInterceptor {
@Pointcut("execution(** com.luchao.spring.test3.service.impl.*.*(..))")
private void anyMethod(){}//定义一个切入点 @Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name);
System.out.println("前置通知");
} @AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
} @After("anyMethod()")
public void after(JoinPoint point){ System.out.println("最终通知");
} @AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("例外通知");
} @Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Method targetMethod = methodSignature.getMethod();
// System.out.println("classname:" + targetMethod.getDeclaringClass().getName());
// System.out.println("superclass:" + targetMethod.getDeclaringClass().getSuperclass().getName());
// System.out.println("isinterface:" + targetMethod.getDeclaringClass().isInterface());
// System.out.println("target:" + pjp.getTarget().getClass().getName());
// System.out.println("proxy:" + pjp.getThis().getClass().getName());
// System.out.println("method:" + targetMethod.getName()); Class[] parameterTypes = new Class[pjp.getArgs().length];
Object[] args = pjp.getArgs();
for(int i=0; i<args.length; i++) {
if(args[i] != null) {
parameterTypes[i] = args[i].getClass();
}else {
parameterTypes[i] = null;
}
}
//获取代理方法对象
String methodName = pjp.getSignature().getName();
Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes); if(method.isAnnotationPresent(LogAnnotation.class)){
System.out.println("存在1");
}
//获取实际方法对象,可以获取方法注解等
Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes()); if(realMethod.isAnnotationPresent(LogAnnotation.class)){
realMethod.getAnnotation(LogAnnotation.class).operateDescribe();
System.out.println("存在2");
} System.out.println("进入环绕通知");
Object object = pjp.proceed();//执行该方法
System.out.println("退出方法");
return object;
}
}
配置类:
@Configurable
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.luchao.spring.test3")
public class test3Config { @Bean
public MyInterceptor myInterceptor(){
return new MyInterceptor();
} @Bean
public EncoreableIntroducer encoreableIntroducer(){
return new EncoreableIntroducer();
}
}
服务类:
@Component
public class PersonServiceBean implements PersonServer { /**
* 保存方法
* @param name
*/
@LogAnnotation(operateModelNm = "测试方法", operateFuncNm = "保存方法")
public void save(String name) {
System.out.println("我是save方法"); } /**
* 更新方法
* @param name
* @param id
*/
public void update(String name, Integer id) {
System.out.println("我是update()方法");
} /**
* 获取方法
* @param id
* @return
*/
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
}
测试方法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = test3Config.class)
public class SpringAOPTest { @Autowired
private PersonServer personServer; @Test
public void inteceptorTest(){
Encoreable encoreable = (Encoreable)personServer;
encoreable.performEncore();
personServer.save("test");
}
}
在springAOP切面中使用的是代理,所以直接获取的是代理对象,不能获取真实对象的一些信息,如注解等。
//获取代理方法对象
String methodName = pjp.getSignature().getName();
Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes);
如果要获取真实对象,获取注解的信息,可以方便我们进行判断记录。
//获取实际方法对象,可以获取方法注解等
Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
这样就完成了一个简单的操作日志记录demo。另外,如果不是讲某个方法设置切点,可以ant风格的切点切入方式,设置多个或所有方法。
springAOP记录用户操作日志的更多相关文章
- RabbitMQ实战场景(一):异步记录用户操作日志
传统的项目开发中业务流程以串行方式,执行了模块1—>模块2–>模块3 而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点: (1)整个流程的执行响应等待时间比较长; ...
- ssm 项目记录用户操作日志和异常日志
借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...
- Spring AOP使用注解记录用户操作日志
最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...
- linux 记录用户操作日志
将以下加入到/etc/profile 最后 history USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]/ ...
- 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...
- 基于SqlSugar的开发框架循序渐进介绍(8)-- 在基类函数封装实现用户操作日志记录
在我们对数据进行重要修改调整的时候,往往需要跟踪记录好用户操作日志.一般来说,如对重要表记录的插入.修改.删除都需要记录下来,由于用户操作日志会带来一定的额外消耗,因此我们通过配置的方式来决定记录那些 ...
- 我使用Spring AOP实现了用户操作日志功能
我使用Spring AOP实现了用户操作日志功能 今天答辩完了,复盘了一下系统,发现还是有一些东西值得拿出来和大家分享一下. 需求分析 系统需要对用户的操作进行记录,方便未来溯源 首先想到的就是在每个 ...
- mysql颠覆实战笔记(三)-- 用户登录(二):保存用户操作日志的方法
版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...
- 利用Hibernate监听器实现用户操作日志
网上搜索发现,实现用户操作日志的方式有:自定义注解方式.Hibernate拦截器方式.Hibernate监听器方式等. 1.自定义注解方式较为麻烦,需要进行操作记录的方法均需要添加注解,但是相对的操作 ...
随机推荐
- DICOM-RT:放疗领域中的各种影像
背景: DICOM-RT系列博文着眼于DICOM3.0中对放疗领域的补充标准,即DICOM-RT.为了方便兴许对DICOM-RT中相关IOD.SOP概念的理解,专栏最近做了放疗相关知识点的普及. PS ...
- svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法
今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not finished; run 'cleanup' if it was interrup ...
- 对Attention is all you need 的理解
https://blog.csdn.net/mijiaoxiaosan/article/details/73251443 本文参考的原始论文地址:https://arxiv.org/abs/1706. ...
- Logistic Regression--逻辑回归算法汇总**
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/18/2595410.html 转自别处 有很多与此类似的文章 也不知道谁是原创 因原文由少于错 ...
- Spring(二十二):Spring 事务
事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性. 事务就是一系列的动作,它们被当做一个单独的工作单元.这些动作要么全部完成,要么全部不起作用. 事务的是四个关键 ...
- 在使用Vs2013打开Vs2008的解决方案时出现了以下错误:此版本的应用程序不支持其项目类型(.csproj)
在使用Vs2013打开Vs2008的解决方案时出现了以下错误: 无法打开 因为此版本的应用程序不支持其项目类型(.csproj). 在网络上找到解决方案: 命令行或者Vs自带的命令提示符输入:deve ...
- [Algorithm] Reservoir Sampling
Given a stream of elements too large to store in memory, pick a random element from the stream with ...
- TaskFactory设置并发量
Task对象很多人知道了(使用Task代替ThreadPool和Thread, C#线程篇—Task(任务)和线程池不得不说的秘密(5)) 相对的还有TaskScheduler 这个调度器,可以自定义 ...
- ReactNative踩坑日志——代码执行方式(面向对象)
在ReactNative中,是以面向对象的方式执行代码的.处于同一{}內的代码以对象的形式执行,也就是说,程序虽然会自上而下执行代码,但是它会保证当前整个代码块內的语句执行完毕才执行下一代码块. 举个 ...
- SpringMVC学习笔记四:数据绑定
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831344.html 参考:http://www.cnblogs.com/HD/p/4107674.html ...