21.AspectJ AOP开发(XML方式)

 

目标类
UserDaoImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.course.dao.impl;

/**
* 目标类
*/
public class UserDaoImpl {

public void addUser(String userName) {
System.out.println("Save user, userName = " + userName);

}

public void deleteUser(Integer userId) {
System.out.println("delete user, userId = " + userId);
int[] a = new int[10];
a[10] = 20;
}

}

目标类
TimeConsumption.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.course.experiment;

public class TimeConsumption {

public void circulation() {
int sum = 0;
for (int i = 0; i < 100; i++) {
sum = sum + i;
}
System.out.println("sum = " + sum);
}
}

切面类
MyDMLAdvice.java

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
package com.course.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyDMLAdvice {

/*
* 前置通知
*/
public void openTransaction() {
System.out.println("open transaction");
}

/*
* 后置通知
*/
public void commitTransaction() {
System.out.println("commit transaction");
}

/*
* 环绕通知
*/
public void dmlTransaction(ProceedingJoinPoint pj){

try {
openTransaction();
//执行目标对象的方法
pj.proceed();
commitTransaction();
} catch (Throwable e) {
e.printStackTrace();
}

}

/*
* 异常通知
*/
public void afterThrowing(Throwable throwable) throws Exception {
System.out.println("产生异常:" + throwable.getMessage());
}

}

切面类
MyTimeConsumptionAdvice.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.course.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyTimeConsumptionAdvice {

/*
* 计算程序执行时间(环绕通知)
*/
public void computeTimeConsumption(ProceedingJoinPoint pj){
try {
long startTime = System.currentTimeMillis();
//执行目标对象的方法
pj.proceed();
long endTime = System.currentTimeMillis();
System.out.println("time consumption = " + (endTime - startTime) + " ms");
} catch (Throwable e) {
e.printStackTrace();
}
}
}

测试类

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
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.course.dao.impl.UserDaoImpl;
import com.course.experiment.TimeConsumption;

public class TestMain {

public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

UserDaoImpl userDaoImpl = (UserDaoImpl) applicationContext.getBean("myUserDaoImpl");

/*
* 前置通知
* 后置通知
* 环绕通知
*/
userDaoImpl.addUser("user1");
// open transaction
// Save user, userName = user1
// commit transaction

/*
* 异常通知
*/
userDaoImpl.deleteUser(2);
// delete user, userId = 2
// 产生异常:Index 10 out of bounds for length 10

/*
* 计算程序执行耗时
*/
TimeConsumption timeConsumption = applicationContext.getBean(TimeConsumption.class);
timeConsumption.circulation();
// sum = 4950
// time consumption = 28 ms
}
}

applicationContext.xml

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
<?xml version="1.0" encoding="UTF-8"?>
<!-- 头文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">


<!-- 声明目标类 -->
<bean id="myUserDaoImpl" class="com.course.dao.impl.UserDaoImpl"></bean>
<bean id="timeConsumption" class="com.course.experiment.TimeConsumption"></bean>

<!-- 声明切面类 -->
<bean id="myAdvise" class="com.course.advice.MyDMLAdvice"></bean>
<bean id="myTimeConsumptionAdvice" class="com.course.advice.MyTimeConsumptionAdvice"></bean>

<!-- 进行AOP配置 -->
<aop:config>
<aop:aspect ref="myAdvise">
<aop:pointcut expression="execution(* com.course.dao.impl.*.*(..))" id="myPointCut1"/>

<!-- 前置通知 -->
<aop:before method="openTransaction" pointcut-ref="myPointCut1"/>
<!-- 后置通知 -->
<aop:after method="commitTransaction" pointcut-ref="myPointCut1"/>
<!-- 环绕通知 -->
<aop:around method="dmlTransaction" pointcut-ref="myPointCut1"/>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrowing" throwing="throwable" pointcut-ref="myPointCut1"/>
</aop:aspect>

</aop:config>

<aop:config>
<aop:aspect ref="myTimeConsumptionAdvice">
<aop:pointcut expression="execution(* com.course.experiment.*.*(..))" id="myPointCut2"/>

<!-- 计算程序执行时间(环绕通知) -->
<aop:around method="computeTimeConsumption" pointcut-ref="myPointCut2"/>
</aop:aspect>
</aop:config>

</beans>