Spring注解执⾏的默认顺序
this is spring是什么意思
对于同⼀个⽅法,上⾯加了n个注解,如下所⽰。
@AnnotationOne @AnnotationTwo public void test() { ……………………………… } 如果,不加order来强制表⽰顺序的话,这2个注解执⾏的默认顺序是什么样⼦的呢?
经查阅Spring官⽅⽂档,可知:
What happens when multiple pieces of advice all want to run at the same join point? Spring AOP follows the same precedence rules as AspectJ to determine the order of advice execution. The highest precedence advice runs first “on the way in” (so, given two pieces of before advice, the one with highest precedence runs first). “On the way out” from a join point, the highest precedence advice runs last (so, given two pieces of after advice, the one with the highest precedence will run second).
When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise, the order of execution is undefined. You can control the order of execution by specifying precedence. This is done in the normal Spring way by either implementing the Ordered interface in the aspect class or annotating it with the Order annotation.
Given two aspects, the aspect returning the lower value Value() (or the annotation value) has the higher precedence.
When two pieces of advice defined in the same aspect both need to run at the same join point, the ordering is undefined (since there is no way to retrieve the declaration order through reflection for javac-compiled classes). Consider collapsing such advice methods into one advice method per join point in each aspect class or refactor the pieces of advice into separate aspect classes that you can order at the aspect level.
中⽂翻译如下:
当在同⼀Join Point上出现多个Advice时怎么办呢? Spring AOP遵循与AspectJ相同的优先级规则来确定建议执⾏的顺序。在进来时,优先级最⾼的Advice⾸先运⾏,在出去时,优先级最⾼的Advice最后运⾏。
当在不同aspect定义的两条Advice都需要在同⼀Join Point上运⾏时,除⾮专门指定顺序,否则它们的执⾏顺序是不确定的。您可以通过指定优先级来控制执⾏顺序,⽅法有两种:1. 在aspect类中实现Ordered接⼝,2. 使⽤Order注解。通常来
说,Value()(或者注释值)返回较低值的aspect具有较⾼的优先级。
当在相同aspect定义的两条Advice都需要在同⼀Join Point上运⾏时,其执⾏顺序也是不确定的(因为通过对java已编译好的类进⾏反射操作,是⽆法获取声明顺序的)。要么,你可以考虑将这两条Advice合并成⼀个Advice,要么,可以将这两条Advice重构成两个aspect类,就能够使⽤Order来⾃定义顺序了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。