How to order @Aspect

放肆的年华 提交于 2019-12-08 11:06:13

问题


In a (Spring boot) application using a lot of @Aspect

  1. How would you ensure the correct aspects ordering with @Order, knowing that you can't have a central place to define the ordering since these aspects may reside in different libs used by different applications?

  2. How would you list all the @Aspect present in the application context, an their order ?

  3. Does it make sense to use @DeclarePrecedence in a separate @Aspect to ensure the ordering instead of @Order for such a use case ?


回答1:


I am not a Spring user, but AFAIK @DeclarePrecedence is not implemented in Spring AOP, it only affects pure AspectJ aspects, i.e. if you use LTW with AspectJ it would work, otherwise you are stuck with @Order plus special values like Ordered.LOWEST_PRECEDENCE and Ordered.HIGHEST_PRECEDENCE.

IMO if you get problems with aspect precedence your design is flawed. An aspect should implement a cross-cutting concern and be independent of other aspects. Of course there are exceptions to every rule, but those should only affect aspects from one library which could sometimes be interdependent. Aspects from different libs should not make any assumptions about order. So if this is a real problem in your code base and not just a theoretical question - go refactor!



来源:https://stackoverflow.com/questions/50406673/how-to-order-aspect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!