22.AOP开发(注解方式)

 

目标类:
UserDaoImpl.java

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.course.dao.impl;

import org.springframework.stereotype.Component;

/**
* 目标类
* @author Bob
*
*/
@Component
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
13
14
15
package com.course.experiment;

import org.springframework.stereotype.Component;

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

import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyDMLAdvice {

/*
* 前置通知
*/
@Before("execution(* com.course.dao.impl.*.*(..))")
public void openTransaction() {
System.out.println("open transaction");
}

/*
* 后置通知
*/
@After("execution(* com.course.dao.impl.*.*(..))")
public void commitTransaction() {
System.out.println("commit transaction");
}

/*
* 环绕通知
*/
@Around("execution(* com.course.dao.impl.*.*(..))")
public void dmlTransaction(ProceedingJoinPoint pj){

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

}

/*
* 异常通知
*/
@AfterThrowing(value = "execution(* com.course.dao.impl.*.*(..))", throwing = "throwable")
public void afterThrowing(Throwable throwable) throws Exception {
System.out.println("产生异常:" + throwable.getMessage());
}

/*
* 计算程序执行时间(环绕通知)
*/
@Around("execution(* com.course.experiment.TimeConsumption.circulation(..))")
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
43
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("userDaoImpl");

/*
* 前置通知
* 后置通知
* 环绕通知
*/
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 = 23 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
<?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">


<!-- 开启扫描 -->
<!--
<context:component-scan base-package="com.course.dao.impl"></context:component-scan>
<context:component-scan base-package="com.course.experiment"></context:component-scan>
<context:component-scan base-package="com.course.advice"></context:component-scan>
-->
<context:component-scan base-package="com.course.*"></context:component-scan>

<!-- 开启注解的AOP -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>