Spring 启动 - 有没有办法为给定的配置文件禁用 AOP?

Spring boot - is there a way to disable AOP for a given profile?

我在 运行 构建 and/or 运行 测试时遇到了 AOP 问题。我尝试用 @Profile("local", "war") 注释 @Aspect class 以确保只有 "local" 和 "war" 配置文件可以访问该方面,但是当我们需要将该方面的支持添加到另一个配置文件中。我们不希望在那里进行硬编码。

是否有一种干净的方法来禁止 "test" 配置文件访问给定方面 class?

假设方面在 spring:

中被视为 beans

如果它只是关于 "create aspect only if not in profile test",那么你只能选择:

@Profile("!test")

如果您想要更复杂的条件,请继续阅读:

Spring 配置文件在 Spring 3.x 中引入,但在 4.x 中用更灵活的条件重写。

看看@Profile源代码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {

    /**
     * The set of profiles for which the annotated component should be registered.
     */
    String[] value();

}

注意 @ConditionalProfileCondition 作为 "evaluation" 逻辑的实现。

这个 class 的实现非常简单。

您可以像配置文件一样创建自定义注释,调用类似的评估逻辑。