Spring Aop之AspectJ注解配置实现日志管理的方法

2025-05-27 0 67

最近项目要做一个日志功能,我用spring aop的注解方式来实现。

创建日志注解

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
package com.wyj.annotation;

import java.lang.annotation.documented;

import java.lang.annotation.elementtype;

import java.lang.annotation.retention;

import java.lang.annotation.retentionpolicy;

import java.lang.annotation.target;

/**

* 日志注解

*

*

* @author:wangyuanjun

* @date:2016年8月26日 下午8:25:35

*/

@target(elementtype.method)

@retention(retentionpolicy.runtime)

@documented

public @interface syslog {

string action() default "";//动作

}

创建切面通知类

记录操作的方法名,参数和花费的时间,使用环绕通知

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95
package com.wyj.aspect;

import java.lang.reflect.method;

import org.aspectj.lang.proceedingjoinpoint;

import org.aspectj.lang.annotation.around;

import org.aspectj.lang.annotation.aspect;

import org.aspectj.lang.annotation.pointcut;

import org.aspectj.lang.reflect.methodsignature;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.stereotype.component;

import com.wyj.annotation.syslog;

import com.wyj.entity.syslogentity;

import com.wyj.service.syslogservice;

/**

* 日志切面通知

*

*

* @author:wangyuanjun

* @date:2016年8月26日 下午10:28:57

*/

@aspect

@component

public class syslogaspect {

@autowired

private syslogservice syslogservice;

/**

* 切入点

*/

@pointcut("@annotation(com.wyj.annotation.syslog)")

public void pointcut() {}

/**

* 环绕通知

*

* @param joinpoint

* @return

* @throws throwable

*/

@around("pointcut()")

public object aroud(proceedingjoinpoint joinpoint) throws throwable {

// 开始时间

long begintime = system.currenttimemillis();

// 执行目标方法

object result = joinpoint.proceed();

// 执行时长(毫秒)

long time = system.currenttimemillis() - begintime;

// 保存日志

savesyslog(joinpoint, time);

return result;

}

/**

* 保存日志

*

* @param joinpoint

* @param time

*/

private void savesyslog(proceedingjoinpoint joinpoint, long time) {

methodsignature signature = (methodsignature) joinpoint.getsignature();

method method = signature.getmethod();

syslogentity syslogentity = new syslogentity();

syslog syslog = method.getannotation(syslog.class);

if (syslog != null) {

// 注解上的描述

syslogentity.setoperation(syslog.action());

}

// 获取目标类名

string classname = joinpoint.gettarget().getclass().getname();

// 获取方法名

string methodname = signature.getname();

syslogentity.setmethod(classname + "." + methodname + "()");

// 请求的参数

object[] args = joinpoint.getargs();

if (args != null && args.length != 0 && args[0] != null) {

syslogentity.setparams(args[0].tostring());

}

syslogentity.settime(time);

// 保存系统日志

syslogservice.save(syslogentity);

}

}

扫描和启动aop注解

Spring Aop之AspectJ注解配置实现日志管理的方法

日志注解的应用

Spring Aop之AspectJ注解配置实现日志管理的方法

效果

Spring Aop之AspectJ注解配置实现日志管理的方法

以上这篇spring aop之aspectj注解配置实现日志管理的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/wangyuanjun008/article/details/79118790

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring Aop之AspectJ注解配置实现日志管理的方法 https://www.kuaiidc.com/76253.html

相关文章

发表评论
暂无评论