原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://shamrock.blog.51cto.com/2079212/1557743
1
2
3
4
5
6
7
8
9
10
11
|
package
com.spring.aop.impl.xml;
public
interface
ArithmeticCalculator {
public
int
add(
int
i,
int
j);
public
int
sub(
int
i,
int
j);
public
int
mul(
int
i,
int
j);
public
int
div(
int
i,
int
j);
}
|
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
|
package
com.spring.aop.impl.xml;
public
class
ArithmeticCalculatorImpl
implements
ArithmeticCalculator{
@Override
public
int
add(
int
i,
int
j) {
int
result = i + j;
return
result;
}
@Override
public
int
sub(
int
i,
int
j) {
int
result = i - j;
return
result;
}
@Override
public
int
mul(
int
i,
int
j) {
int
result = i * j;
return
result;
}
@Override
public
int
div(
int
i,
int
j) {
int
result = i / j;
return
result;
}
}
|
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
|
package
com.spring.aop.impl.xml;
import
java.util.Arrays;
import
java.util.List;
import
org.aspectj.lang.JoinPoint;
import
org.aspectj.lang.ProceedingJoinPoint;
public
class
LoggingAspect {
public
void
beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object>args = Arrays.asList(joinpoint.getArgs());
System.out.println(
"前置通知:The method "
+ methodName +
" begins with "
+ args);
}
public
void
afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
//List<Object>args = Arrays.asList(joinpoint.getArgs()); 后置通知方法中可以获取到参数
System.out.println(
"后置通知:The method "
+ methodName +
" ends "
);
}
public
void
afterReturnning(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
System.out.println(
"返回通知:The method "
+ methodName +
" ends with "
+ result);
}
public
void
afterThrowing(JoinPoint joinpoint, Exception e) {
String methodName = joinpoint.getSignature().getName();
System.out.println(
"异常通知:The method "
+ methodName +
" occurs exception "
+ e);
}
public
Object aroundMethod(ProceedingJoinPoint point) {
Object result =
null
;
String methodName = point.getSignature().getName();
try
{
//前置通知
System.out.println(
"The method "
+ methodName +
" begins with "
+ Arrays.asList(point.getArgs()));
//执行目标方法
result = point.proceed();
//翻译通知
System.out.println(
"The method "
+ methodName +
" ends with "
+ result);
}
catch
(Throwable e) {
//异常通知
System.out.println(
"The method "
+ methodName +
" occurs exception "
+ e);
throw
new
RuntimeException(e);
}
//后置通知
System.out.println(
"The method "
+ methodName +
" ends"
);
return
result;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.spring.aop.impl.xml;
import
java.util.Arrays;
import
org.aspectj.lang.JoinPoint;
public
class
ValidationAspect {
public
void
validateArgs(JoinPoint joinPoint) {
System.out.println(
"validate:"
+ Arrays.asList(joinPoint.getArgs()));
}
}
|
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
|
<?
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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 配置bean -->
<
bean
id
=
"arithmeticCalculator"
class
=
"com.spring.aop.impl.xml.ArithmeticCalculatorImpl"
></
bean
>
<!-- 配置切面的bean -->
<
bean
id
=
"loggingAspect"
class
=
"com.spring.aop.impl.xml.LoggingAspect"
></
bean
>
<
bean
id
=
"validationAspect"
class
=
"com.spring.aop.impl.xml.ValidationAspect"
></
bean
>
<!-- 配置AOP -->
<
aop:config
>
<!-- 配置切点表达式 -->
<
aop:pointcut
expression
=
"execution(* com.spring.aop.impl.xml.ArithmeticCalculator.*(..))"
id
=
"pointcut"
/>
<!-- 配置切面及通知,使用order指定优先级 -->
<
aop:aspect
ref
=
"loggingAspect"
order
=
"1"
>
<!-- 环绕通知 -->
<!--
<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
-->
<!-- 前置通知 -->
<
aop:before
method
=
"beforeMethod"
pointcut-ref
=
"pointcut"
/>
<!-- 后置通知 -->
<
aop:after
method
=
"afterMethod"
pointcut-ref
=
"pointcut"
/>
<!-- 异常通知 -->
<
aop:after-throwing
method
=
"afterThrowing"
pointcut-ref
=
"pointcut"
throwing
=
"e"
/>
<!-- 返回通知 -->
<
aop:after-returning
method
=
"afterReturnning"
pointcut-ref
=
"pointcut"
returning
=
"result"
/>
</
aop:aspect
>
<
aop:aspect
ref
=
"validationAspect"
order
=
"2"
>
<!-- 前置通知 -->
<
aop:before
method
=
"validateArgs"
pointcut-ref
=
"pointcut"
/>
</
aop:aspect
>
</
aop:config
>
</
beans
>
|
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.spring.aop.impl.xml;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
Main {
public
static
void
main(String[] args) {
//创建spring IOC容器
ApplicationContext applicationContext =
new
ClassPathXmlApplicationContext(
"applicationContext-xml.xml"
);
//从IOC容器中获取bean实例
ArithmeticCalculator arithmeticCalculator = applicationContext.getBean(ArithmeticCalculator.
class
);
int
result = arithmeticCalculator.add(
4
,
6
);
System.out.println(result);
result = arithmeticCalculator.sub(
4
,
6
);
System.out.println(result);
System.out.println(result);
result = arithmeticCalculator.mul(
4
,
6
);
System.out.println(result);
System.out.println(result);
result = arithmeticCalculator.div(
4
,
0
);
System.out.println(result);
}
}
|