Spring AOP日志管理

释放双眼,带上耳机,听听看~!

AOP的介绍

AOP

Aspect-OrientedProgramming
,面向方面编程)

AOP
的几个概念

切面
(Aspect)
:切面就是一个关注点的模块化,如事务管理、日志管理、权限管理等;

连接点
(Joinpoint)
:程序执行时的某个特定的点,在
Spring
中就是一个方法的执行;

通知
(Advice)
:通知就是在切面的某个连接点上执行的操作,也就是事务管理、日志管理等;

切入点
(Pointcut)
:切入点就是描述某一类选定的连接点,也就是指定某一类要织入通知的方法;

目标对象
(Target)
:就是被
AOP
动态代理的目标对象;

这里使用登录功能做日志管理案例

概要

1

LoginService LogService TestMain

2
)用
Spring
管理
LoginService

LogService
的对象

3
)确定哪些连接点是切入点,在配置文件中

4
)将
LogService
封装为通知

5
)将通知植入到切入

6
)客户端调用目标点

Step1 新建登录业务接口以及实现类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1package com.aaron.log;
2/**
3
4 * @Author Aaron
5
6 * @Date创建时间:2016-1-13
7
8 * @Version 1.0  
9
10 *
11
12 * @Project_Package_Description SpringQuartzDemo || com.aaron.log
13
14 * @Function_Description登录业务逻辑
15
16*
17
18 */
19
20public interface ILoginService {
21public boolean login(String name, String password);
22}
23

 


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
1package com.aaron.log;
2/**
3
4 * @Author Aaron
5
6 * @Date创建时间:2016-1-13
7
8 * @Version 1.0  
9
10 *
11
12 * @Project_Package_Description SpringQuartzDemo || com.aaron.log
13
14 * @Function_Description登录业务逻辑接口实现类
15
16*
17
18 */
19
20public class LoginServiceImpl implements ILoginService {
21public boolean login(String userName, String password) {
22 System.out.println("用户登录信息:" + userName + "," + password);
23return true;
24 }
25}
26

Step2 新建日志管理接口以及实现类

 


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
1package com.aaron.log;
2import org.aspectj.lang.JoinPoint;
3/**
4
5 * @Author Aaron
6
7 * @Date创建时间:2016-1-13
8
9 * @Version 1.0  
10
11 *
12
13 * @Project_Package_Description SpringQuartzDemo || com.aaron.log
14
15* @Function_Description日志接口
16
17*
18
19 */
20
21public interface ILogService {
22
23//无参的日志方法
24public void beforeLog();
25
26//有参的日志方法
27public void Log(JoinPoint point);
28
29//有参有返回值的方法
30public void afterLog(JoinPoint point,Object returnObj);
31
32}
33

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
1package com.aaron.log;
2import org.aspectj.lang.JoinPoint;
3/**
4
5 * @Author Aaron
6
7 * @Date创建时间:2016-1-13
8
9 * @Version 1.0
10
11 *  
12
13 * @Project_Package_Description SpringQuartzDemo || com.aaron.log
14
15 * @Function_Description记录日志接口实现类
16
17*  
18
19 */
20
21public class LogServiceImpl implements ILogService {
22    // 无参方法
23    public void beforeLog() {
24        System.out.println("*************开始记录日志*******************");
25    }
26
27    // 有参无返回值的方法
28    public void Log(JoinPoint point) {
29        // 此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
30        Object[] args = point.getArgs();
31        System.out.print("目标参数列表:");
32        for (Object obj : args) {
33            System.out.print(obj + ",");
34        }
35        System.out.println();
36    }  
37
38    // 有参并有返回值的方法
39
40    public void afterLog(JoinPoint point, Object returnObj) {
41        // 此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象
42        Object[] args = point.getArgs();
43        System.out.print("目标参数列表:");
44        for (Object obj : args) {
45            System.out.print(obj + ",");
46        }
47        System.out.println();
48        System.out.println("执行结果是:" + returnObj);
49        System.out.println("*************结束记录日志*******************");
50    }
51}
52

Step3 在applicationContext.xml文件中配置AOP

Spring AOP日志管理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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
1<?xml version="1.0" encoding="UTF-8"?>
2
3<beans xmlns="http://www.springframework.org/schema/beans"
4
5    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6
7    xmlns:p="http://www.springframework.org/schema/p"
8
9    xsi:schemaLocation="http://www.springframework.org/schema/beans  
10
11 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
12
13 http://www.springframework.org/schema/aop  
14
15 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
16
17    <!-- 启动触发器的配置开始 -->
18
19    <bean name="startQuertz" lazy-init="false" autowire="no"
20
21        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
22
23        <property name="triggers">
24
25            <list>
26
27                <ref bean="myJobTrigger" />
28
29            </list>
30
31        </property>
32
33    </bean>
34
35    <!-- 启动触发器的配置结束 -->
36
37
38
39    <!-- 调度的配置开始 -->
40
41    <!-- quartz-1.8以前的配置 <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
42
43        <property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property  
44
45        name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> -->
46
47    <!-- quartz-2.x的配置 -->
48
49    <bean id="myJobTrigger"
50
51        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
52
53        <property name="jobDetail">
54
55            <ref bean="myJobDetail" />
56
57        </property>
58
59        <property name="cronExpression">
60
61            <value>0 0/1 * * * ?</value>
62
63        </property>
64
65    </bean>
66
67    <!-- 调度的配置结束 -->
68
69
70
71    <!-- job的配置开始 -->
72
73    <bean id="myJobDetail"
74
75        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
76
77        <property name="targetObject">
78
79            <ref bean="myJob" />
80
81        </property>
82
83        <property name="targetMethod">
84
85            <value>work</value>
86
87        </property>
88
89    </bean>
90
91    <!-- job的配置结束 -->
92
93
94
95    <!-- 工作的bean -->
96
97    <bean id="myJob" class="com.tgb.lk.demo.quartz.MyJob" />
98
99
100
101    <bean id="logService" class="com.aaron.log.LogServiceImpl"></bean>
102
103    <bean id="loginService" class="com.aaron.log.LoginServiceImpl"></bean>
104
105    <aop:config>
106
107        <!-- 切入点 -->
108
109        <aop:pointcut expression="execution(* com.aaron.log.LoginServiceImpl.*(..))"
110
111            id="myPointcut" />
112
113        <!-- 切面:将哪个对象中的哪个方法,织入到哪个切入点 -->
114
115        <aop:aspect id="dd" ref="logService">
116
117            <!-- 前置通知 -->
118
119            <aop:before method="beforeLog" pointcut-ref="myPointcut" />
120
121            <aop:after method="Log" pointcut-ref="myPointcut"/>
122
123            <aop:after-returning method="afterLog" returning="returnObj" pointcut-ref="myPointcut" />
124
125        </aop:aspect>
126
127    </aop:config>
128
129</beans>
130

applicationContext.xml

Step4 测试方法


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
1package com.aaron.log;
2import org.springframework.context.ApplicationContext;
3import org.springframework.context.support.ClassPathXmlApplicationContext;
4/**
5
6* @Author Aaron
7
8* @Date 创建时间:2016-1-13
9
10* @Version 1.0
11
12*
13
14* @Project_Package_Description SpringQuartzDemo || com.aaron.log
15
16* @Function_Description 测试
17
18*
19
20*/
21
22public class TestMain {
23    public static void main(String[] args) {
24        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:config/spring/applicationContext.xml");
25        ILoginService loginService = (ILoginService) ctx.getBean("loginService");
26        loginService.login("aaron", "123456");
27    }
28}
29

Step5 运行,看后台

Spring AOP日志管理

 

*PS
:使用注解可以参考别人写的这篇博客
http://blog.csdn.net/oathevil/article/details/7288867 *

 

转载于:https://www.cnblogs.com/haaron/p/5127835.html

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ vector

2022-1-11 12:36:11

安全资讯

遭遇芯片短缺,马斯克:你们不懂生产汽车有多难

2021-6-10 17:25:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索