项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用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记录用户操作日志的更多相关文章

  1. RabbitMQ实战场景(一):异步记录用户操作日志

    传统的项目开发中业务流程以串行方式,执行了模块1—>模块2–>模块3 而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点: (1)整个流程的执行响应等待时间比较长; ...

  2. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  3. Spring AOP使用注解记录用户操作日志

    最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...

  4. linux 记录用户操作日志

    将以下加入到/etc/profile 最后 history USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]/ ...

  5. 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录

    在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...

  6. 基于SqlSugar的开发框架循序渐进介绍(8)-- 在基类函数封装实现用户操作日志记录

    在我们对数据进行重要修改调整的时候,往往需要跟踪记录好用户操作日志.一般来说,如对重要表记录的插入.修改.删除都需要记录下来,由于用户操作日志会带来一定的额外消耗,因此我们通过配置的方式来决定记录那些 ...

  7. 我使用Spring AOP实现了用户操作日志功能

    我使用Spring AOP实现了用户操作日志功能 今天答辩完了,复盘了一下系统,发现还是有一些东西值得拿出来和大家分享一下. 需求分析 系统需要对用户的操作进行记录,方便未来溯源 首先想到的就是在每个 ...

  8. mysql颠覆实战笔记(三)-- 用户登录(二):保存用户操作日志的方法

    版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...

  9. 利用Hibernate监听器实现用户操作日志

    网上搜索发现,实现用户操作日志的方式有:自定义注解方式.Hibernate拦截器方式.Hibernate监听器方式等. 1.自定义注解方式较为麻烦,需要进行操作记录的方法均需要添加注解,但是相对的操作 ...

随机推荐

  1. MFC中如何显示颜色选择对话框

    其实很简单,使用MFC现有的类CColorDialog 即可实现 核心代码如下: void CCColorDialogView::OnGraphSetting() { CColorDialog m_s ...

  2. Word Embedding与Word2Vec

    http://blog.csdn.net/baimafujinji/article/details/77836142 一.数学上的“嵌入”(Embedding) Embed这个词,英文的释义为, fi ...

  3. MySQL数据库select语句的使用方法

    select语句可 以用回车分隔 $sql="select * from article where id=1"和 $sql="select * from article ...

  4. (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  5. glValidateProgram只用于调试

    glValidateProgram应该只用于调试,用于release版本中会影响性能.以下是详细描述:   Before doing so, however, we might want to che ...

  6. 4、Cocos2dx 3.0游戏开发找小三之Hello World 分析

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27186557 Hello World 分析 打开新 ...

  7. 火星坐标、百度坐标、WGS84坐标转换代码(JS、python版)

    火星坐标.百度坐标.WGS84坐标转换代码(JS.python版) 一.JS版本源码 github:https://github.com/wandergis/coordTransform /** * ...

  8. KafkaOffsetMonitor 安装

    KafkaOffsetMonitor 安装   1,下载KafkaOffsetMonitor-assembly-0.2.0.jar 2,启动 步骤1:启动ZK(DN1-DN3节点) zkServer. ...

  9. VO、DTO与领域模型的概念

    业务对象模型(也叫领域模型 domain model)是描述业务用例实现的对象模型.它是对业务角色和业务实体之间应该如何联系和协作以执行业务的一种抽象.业务对象模型从业务角色内部的观点定义了业务用例. ...

  10. VMware vSphere can virtualize itself + 64-bit nested guests

    Running VMware ESXi inside a virtual machine is a great way to experiment with different configurati ...