在运行构建和/或运行测试时,我一直遇到 AOP 问题。我尝试使用 @Profile("local", "war")
注释 @Aspect
类,以确保该方面只能由“local”和“war”访问“配置文件,但是当我们需要将该方面支持添加到另一个配置文件时,可能会出现一种情况。我们不希望将其硬编码在那里。
是否有一种干净的方法来禁止例如“测试”配置文件访问给定的方面类?
最佳答案
假设,方面被视为 Spring 的 bean :
如果它只是关于“仅在不在配置文件测试中时创建方面”,那么您只需选择:
@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();
}
请注意 @Conditional
和 ProfileCondition
作为“评估”逻辑的实现。
这个类的实现非常简单。
您可以创建自定义注释,就像配置文件一样,它将调用类似的评估逻辑。
关于java - Spring boot - 有没有办法为给定的配置文件禁用 AOP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58855106/