项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用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. Java归去来第1集:手动给Eclipse配置Maven环境

    一.Eclipse配置Maven 1.1.下载Maven http://maven.apache.org/download.cgi,选择对应的版本,window下载apache-maven-3.5.3 ...

  2. java 让图片变黑白

    import java.awt.Image;import java.awt.color.ColorSpace;import java.awt.image.BufferedImage;import ja ...

  3. PHP Manager for IIS

    SOAP error on IIS8 Registering new PHP version sets bad values set for FastCGI activityTimeout, requ ...

  4. Dubbo-Fail to decode request due to: RpcInvocation

    使用Dubbo进行服务化,遇到如下错误: Caused by: com.alibaba.dubbo.remoting.RemotingException: Fail to decode request ...

  5. chrome插件编写基本入门

    chrome插件编写基本入门  http://igeekbar.com/igeekbar/post/331.htm #精选JAVASCRIPTCHROME 作为一名程序猿,怎么能不会写chrome插件 ...

  6. Android 设计原则【转载+整理】

    原文地址 本文内容 吸引我的眼球 简化我的生活 让我眼前一亮 在使用过大量 Android APP 后,你会发现,遵循了下面这些原则的 APP 将会有更好的用户体验. 我们知道,往往国企的那些软件,都 ...

  7. SuperMap iDesktop之导入数据

    SuperMap作为一个平台软件有自己的数据格式,现要将ESRI的SHP数据导入到SuperMap的udb数据库中,可以完成导入,但也不得不说几点问题. 下面是ArcGIS中批量导入SHP的操作界面. ...

  8. WinForm程序开发

    WinForm程序开发------------------------------主要页面----------------------------------BaseForm.cs    基类,用于派 ...

  9. loadscript加载

    function load_script(xyUrl, callback){ var head = document.getElementsByTagName('head')[0]; var scri ...

  10. CSS 之 伪类及伪元素

    伪类和伪元素用起来非常的方便,在查阅资料及测试后整理下来. 一.伪类 CSS 伪类用于向某些选择器添加特殊的效果.伪类对元素进行分类是基于特征(characteristics)而不是它们的名字.属性或 ...