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.自定义注解方式较为麻烦,需要进行操作记录的方法均需要添加注解,但是相对的操作 ...
随机推荐
- Android -- TypedArray
当我们自定义View的时候,在给View赋值一些长度宽度的时候,一般都是在layout布局文件中进行的.,比如android:layout_height="wrap_content" ...
- 【Spark】Spark Streaming + Kafka direct 的 offset 存入Zookeeper并重用
Spark Streaming + Kafka direct 的 offset 存入Zookeeper并重用 streaming offset设置_百度搜索 将 Spark Streaming + K ...
- TOJ 1220 填数字游戏 / 深搜
填数字游戏 时间限制(普通/Java):1000MS/10000MS 运行内存限制:65536KByte 描述 有个小游戏,让你填写以下方框,要求: a1+a2+a3+M=b1+b2+b3+M ...
- linux下获取硬盘使用情况[总结]
1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场景,我们需要在写日志过程中判断硬盘空间 ...
- JqueryValidate表单相同Name不校验问题解决
在使用Jquery validate中遇到一个问题,当表单元素有name相同字段时,validate只校验表单元素name第一个值是否通过校验,比如 <input type="text ...
- ZooKeeper启动报错 JAVA_HOME is incorrectly set
解决办法:在zkEnv.cmd文件中直接写死调用的jdk路径 set JAVA_HOME="D:\Program Files\Java7\jdk1.7.0_51" if not e ...
- Python连接MySQL的实例代码
Python连接MySQL的实例代码 MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/ 下载解压缩后放到%Python_HOME% ...
- maven 配置篇 之pom.xml
http://www.blogjava.net/zyl/archive/2006/12/30/91055.html http://maven.apache.org/pom.html的翻译. m ...
- 牛客网-《剑指offer》-调整数组顺序使奇数位于偶数前面
题目:http://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593 C++ class Solution { public: vo ...
- 微信小程序 - 配置普通二维码跳小程序
普通二维码跳小程序规则: https://developers.weixin.qq.com/miniprogram/introduction/qrcode.html#%E5%8A%9F%E8%83%B ...