本文介绍了Spring Aspects/Advisors 的初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法配置 Spring 中不同方面(或者更准确地说是它们对应的顾问)的初始化方式?请注意,我说的不是通知顺序,而是方面的初始化.

Is there a way to configure in which way different Aspects (or their corresponding Advisors to be more precise) in Spring are initialized? Note that I am not talking about the advice order, but the initialization of the aspects.

这个问题的背景是一个用例,Aspect A 依赖于 Service X,应该由 Aspect B 提供建议.但是,由于 Aspect A 首先被初始化并强制创建 Service XAspect B 不会应用于服务.

The background of this question is a use case with Aspect A dependent on Service X which should be advised by Aspect B. However, as Aspect A gets initialized first and enforces the creation of Service X, Aspect B is not applied to the service.

如何在不使 Service X 依赖于 Aspect B 的情况下更改初始化顺序(这将是一种解决方法,因为它强制执行所需的顺序,但服务不应必须处理它的方面......)?

How can I change the initialization order without making Service X dependent on Aspect B (this would be a workaround as it enforces the desired order, but a service should never have to deal with its aspects...)?

如果你想了解我的问题,这是另一个问题,让我提出这个问题.

If you want the big picture of my issue, here is another question which led me to asking this one.

推荐答案

方面通常只是一个带有 @Aspect 注释的类,该类也用 @Component 或有一个对应的 定义.换句话说,它只是一个 bean.

An aspect is typically just an @Aspect annotated class that is also annotated with @Component or that has a corresponding <bean> definition. In other words, it is just a bean.

定义 bean 初始化顺序的唯一方法是使用 depends-on 属性(或 @DependsOn)导入上下文.

The only ways to define the order that beans should be initialized is with the depends-on property (or @DependsOn) or by playing with the order in which you import contexts.

这篇关于Spring Aspects/Advisors 的初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 17:24