gpt4 book ai didi

java - 使用 AOP 拦截私有(private)注释方法

转载 作者:行者123 更新时间:2023-11-30 11:12:22 28 4
gpt4 key购买 nike

我有一个自定义注释:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FeatureSwitch {

String featureName();

}

我用下面的方面拦截它,并用它来检查某个功能是打开还是关闭。如果该功能关闭,我将抛出异常。

看点:

@Aspect
public class FeatureSwitchAspect {

private final FeatureSwitchConfigurationApi featureSwitchConfigurationApi;

public FeatureSwitchAspect(final FeatureSwitchConfigurationApi featureSwitchConfigurationApi) {
this.featureSwitchConfigurationApi = featureSwitchConfigurationApi;
}

@Before("@annotation(featureSwitch)")
public void checkFeatureSwitch(final FeatureSwitch featureSwitch) {
final String featureName = featureSwitch.featureName();
Boolean featSwitch = featureSwitchConfigurationApi.isFeatureActive(featureName);
if (!featSwitch) {
throw new FeatureSwitchOffException();
}
}
}

我遇到的问题是行为似乎不一致。当我从不同的类调用方法时,这似乎符合预期,但如果我调用带注释的私有(private)方法,则不会发生拦截。我配置不正确吗?如有任何建议,我们将不胜感激。

最佳答案

来自类内部的方法调用不适用于基于代理的 AOP。

由于您正在使用关键字 this(它是指向您的原始对象而不是包装它的代理对象的指针),您将直接调用包装方法 - 从而绕过代码作为 AOP 的结果添加。

关于java - 使用 AOP 拦截私有(private)注释方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931113/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com