前置通知[before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。
正常返回通知[after returning advice]:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行。
异常返回通知[after throwing advice]:在连接点抛出异常后执行。
返回通知[after (finally) advice]:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。
环绕通知[around advice]:环绕通知围绕在连接点前后,比如一个方法调用的前后。这是最强大的通知类型,能在方法调用前后自定义一些操作。
环绕通知还需要负责决定是继续处理join point(调用proceedingjoinpoint的proceed方法)还是中断执行。
接下来通过编写示例程序来测试一下五种通知类型:
定义接口
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.chenqa.springaop.example.service;
public interface bankservice {
/**
* 模拟的银行转账
* @param from 出账人
* @param to 入账人
* @param account 转账金额
* @return
*/
public boolean transfer(string form, string to, double account);
}
|
编写实现类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.chenqa.springaop.example.service.impl;
import com.chenqa.springaop.example.service.bankservice;
public class bcmbankserviceimpl implements bankservice {
public boolean transfer(string form, string to, double account) {
if(account<100) {
throw new illegalargumentexception("最低转账金额不能低于100元");
}
system.out.println(form+"向"+to+"交行账户转账"+account+"元");
return false;
}
}
|
修改spring配置文件,添加以下内容:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- bankservice bean -->
<bean id="bankservice" class="com.chenqa.springaop.example.service.impl.bcmbankserviceimpl"/>
<!-- 切面 -->
<bean id="myaspect" class="com.chenqa.springaop.example.aspect.myaspect"/>
<!-- aop配置 -->
<aop:config>
<aop:aspect ref="myaspect">
<aop:pointcut expression="execution(* com.chenqa.springaop.example.service.impl.*.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="afterreturning" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterthrowing" pointcut-ref="pointcut"/>
<aop:around method="around" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
|
编写测试程序
|
1
2
3
|
applicationcontext context = new classpathxmlapplicationcontext("spring-aop.xml");
bankservice bankservice = context.getbean("bankservice", bankservice.class);
bankservice.transfer("张三", "李四", 200);
|
执行后输出:
将测试程序中的200改成50,再执行后输出:
通过测试结果可以看出,五种通知的执行顺序为:
前置通知→环绕通知→正常返回通知/异常返回通知→返回通知,可以多次执行来查看。
情况一: 一个方法只被一个aspect类拦截
当一个方法只被一个aspect拦截时,这个aspect中的不同advice是按照怎样的顺序进行执行的呢?请看:
添加pointcut类
该pointcut用来拦截test包下的所有类中的所有方法。
|
1
2
3
4
5
6
7
8
9
10
|
package test;
import org.aspectj.lang.annotation.pointcut;
public class pointcuts {
@pointcut(value = "within(test.*)")
public void aopdemo() {
}
}
|
添加aspect类
该类中的advice将会用到上面的pointcut,使用方法请看各个advice的value属性。
|
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
|
package test;
import org.aspectj.lang.joinpoint;
import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.component;
@component
@aspect
public class aspect1 {
@before(value = "test.pointcuts.aopdemo()")
public void before(joinpoint joinpoint) {
system.out.println("[aspect1] before advise");
}
@around(value = "test.pointcuts.aopdemo()")
public void around(proceedingjoinpoint pjp) throws throwable{
system.out.println("[aspect1] around advise 1");
pjp.proceed();
system.out.println("[aspect1] around advise2");
}
@afterreturning(value = "test.pointcuts.aopdemo()")
public void afterreturning(joinpoint joinpoint) {
system.out.println("[aspect1] afterreturning advise");
}
@afterthrowing(value = "test.pointcuts.aopdemo()")
public void afterthrowing(joinpoint joinpoint) {
system.out.println("[aspect1] afterthrowing advise");
}
@after(value = "test.pointcuts.aopdemo()")
public void after(joinpoint joinpoint) {
system.out.println("[aspect1] after advise");
}
}
|
添加测试用controller
添加一个用于测试的controller,这个controller中只有一个方法,但是它会根据参数值的不同,会作出不同的处理:一种是正常返回一个对象,一种是抛出异常(因为我们要测试@afterthrowing这个advice)
|
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
|
package test;
import test.exception.testexception;
import org.springframework.http.httpstatus;
import org.springframework.web.bind.annotation.*;
@restcontroller
@requestmapping(value = "/aop")
public class aoptestcontroller {
@responsestatus(httpstatus.ok)
@requestmapping(value = "/test", method = requestmethod.get)
public result test(@requestparam boolean throwexception) {
// case 1
if (throwexception) {
system.out.println("throw an exception");
throw new testexception("mock a server exception");
}
// case 2
system.out.println("test ok");
return new result() {{
this.setid(111);
this.setname("mock a result");
}};
}
public static class result {
private int id;
private string name;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
}
}
|
测试 正常情况
在浏览器直接输入以下的url,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=false1
我们会看到输出的结果是:
|
1
2
3
4
5
6
|
[aspect1] around advise 1
[aspect1] before advise
test ok
[aspect1] around advise2
[aspect1] after advise
[aspect1] afterreturning advise
|
测试 异常情况
在浏览器中直接输入以下的url,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=true1
我们会看到输出的结果是:
|
1
2
3
4
5
|
[aspect1] around advise 1
[aspect1] before advise
throw an exception
[aspect1] after advise
[aspect1] afterthrowing advise
|
结论
在一个方法只被一个aspect类拦截时,aspect类内部的 advice 将按照以下的顺序进行执行:
正常情况:
异常情况:
情况二: 同一个方法被多个aspect类拦截
此处举例为被两个aspect类拦截。
有些情况下,对于两个不同的aspect类,不管它们的advice使用的是同一个pointcut,还是不同的pointcut,都有可能导致同一个方法被多个aspect类拦截。那么,在这种情况下,这多个aspect类中的advice又是按照怎样的顺序进行执行的呢?请看:
pointcut类保持不变
添加一个新的aspect类
|
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
|
package test;
import org.aspectj.lang.joinpoint;
import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.component;
@component
@aspect
public class aspect2 {
@before(value = "test.pointcuts.aopdemo()")
public void before(joinpoint joinpoint) {
system.out.println("[aspect2] before advise");
}
@around(value = "test.pointcuts.aopdemo()")
public void around(proceedingjoinpoint pjp) throws throwable{
system.out.println("[aspect2] around advise 1");
pjp.proceed();
system.out.println("[aspect2] around advise2");
}
@afterreturning(value = "test.pointcuts.aopdemo()")
public void afterreturning(joinpoint joinpoint) {
system.out.println("[aspect2] afterreturning advise");
}
@afterthrowing(value = "test.pointcuts.aopdemo()")
public void afterthrowing(joinpoint joinpoint) {
system.out.println("[aspect2] afterthrowing advise");
}
@after(value = "test.pointcuts.aopdemo()")
public void after(joinpoint joinpoint) {
system.out.println("[aspect2] after advise");
}
}
|
测试用controller也不变
还是使用上面的那个controller。但是现在 aspect1 和 aspect2 都会拦截该controller中的方法。
下面继续进行测试!
测试 正常情况
在浏览器直接输入以下的url,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=false1
我们会看到输出的结果是:
|
1
2
3
4
5
6
7
8
9
10
11
|
[aspect2] around advise 1
[aspect2] before advise
[aspect1] around advise 1
[aspect1] before advise
test ok
[aspect1] around advise2
[aspect1] after advise
[aspect1] afterreturning advise
[aspect2] around advise2
[aspect2] after advise
[aspect2] afterreturning advise
|
但是这个时候,我不能下定论说 aspect2 肯定就比 aspect1 先执行。
不信?你把服务务器重新启动一下,再试试,说不定你就会看到如下的执行结果:
|
1
2
3
4
5
6
7
8
9
10
11
|
[aspect1] around advise 1
[aspect1] before advise
[aspect2] around advise 1
[aspect2] before advise
test ok
[aspect2] around advise2
[aspect2] after advise
[aspect2] afterreturning advise
[aspect1] around advise2
[aspect1] after advise
[aspect1] afterreturning advise
|
也就是说,这种情况下, aspect1 和 aspect2 的执行顺序是未知的。那怎么解决呢?不急,下面会给出解决方案。
测试 异常情况
在浏览器中直接输入以下的url,回车:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=true1
我们会看到输出的结果是:
|
1
2
3
4
5
6
7
8
9
|
[aspect2] around advise 1
[aspect2] before advise
[aspect1] around advise 1
[aspect1] before advise
throw an exception
[aspect1] after advise
[aspect1] afterthrowing advise
[aspect2] after advise
[aspect2] afterthrowing advise
|
同样地,如果把服务器重启,然后再测试的话,就可能会看到如下的结果:
|
1
2
3
4
5
6
7
8
9
|
[aspect1] around advise 1
[aspect1] before advise
[aspect2] around advise 1
[aspect2] before advise
throw an exception
[aspect2] after advise
[aspect2] afterthrowing advise
[aspect1] after advise
[aspect1] afterthrowing advise
|
也就是说,同样地,异常情况下, aspect1 和 aspect2 的执行顺序也是未定的。
那么在 情况二 下,如何指定每个 aspect 的执行顺序呢?
方法有两种:
- 实现org.springframework.core.ordered接口,实现它的getorder()方法
- 给aspect添加@order注解,该注解全称为:org.springframework.core.annotation.order
不管采用上面的哪种方法,都是值越小的 aspect 越先执行。
比如,我们为 apsect1 和 aspect2 分别添加 @order 注解,如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@order(5)
@component
@aspect
public class aspect1 {
// ...
}
@order(6)
@component
@aspect
public class aspect2 {
// ...
}
|
这样修改之后,可保证不管在任何情况下, aspect1 中的 advice 总是比 aspect2 中的 advice 先执行。如下图所示:
注意点
如果在同一个 aspect 类中,针对同一个 pointcut,定义了两个相同的 advice(比如,定义了两个 @before),那么这两个 advice 的执行顺序是无法确定的,哪怕你给这两个 advice 添加了 @order 这个注解,也不行。这点切记。
对于@around这个advice,不管它有没有返回值,但是必须要方法内部,调用一下pjp.proceed();否则,controller 中的接口将没有机会被执行,从而也导致了@before这个advice不会被触发。比如,我们假设正常情况下,执行顺序为”aspect2 -> apsect1 -> controller”,如果,我们把aspect1中的@around中的pjp.proceed();给删掉,那么,我们看到的输出结果将是:
|
1
2
3
4
5
6
7
8
9
|
[aspect2] around advise 1
[aspect2] before advise
[aspect1] around advise 1
[aspect1] around advise2
[aspect1] after advise
[aspect1] afterreturning advise
[aspect2] around advise2
[aspect2] after advise
[aspect2] afterreturning advise
|
从结果可以发现, controller 中的 接口 未被执行,aspect1 中的@beforeadvice 也未被执行。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/qq_35873847/article/details/78624941
相关文章
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 35
-
2025-05-25 22
-
2025-05-29 93
-
Win10ctrl键失灵怎么办?Win10ctrl键失灵的解决方法
2025-05-27 64 -
2025-05-29 100






