问题
In a (Spring boot) application using a lot of @Aspect
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?How would you list all the
@Aspect
present in the application context, an their order ?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