gpt4 book ai didi

java - Aspectj 和捕获私有(private)或内部方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:18 24 4
gpt4 key购买 nike

我已经用 Spring 配置了 AspectJ,它在“捕获”从类外调用的公共(public)方法时工作正常。现在我想做这样的事情:

public class SomeLogic(){

public boolean someMethod(boolean test){

if(test){
return innerA();
} else {
return innerB();
}
}


private boolean innerA() {// some logic}
private boolean innerA() {// some other logic}

}

SomeLogic 是一个 SpringBean。方法 innerA() 和 innerB() 可以声明为私有(private)或公共(public) - 方法 someMethod() 从 Struts 操作中调用。是否可以使用 AspectJ 捕获从 someMethod() 调用的方法 innerA() 或 innerB() ?

我的配置(基于 XML):

    <aop:aspect id="innerAAspect" ref="INNER_A">
<aop:pointcut id="innerAService" expression="execution(* some.package.SomeLogic.innerA(..))"/>
</aop:aspect>

<aop:aspect id="innerAAround" ref="INNER_A">
<aop:around pointcut-ref="innerAService" method="proceed"/>
</aop:aspect>


<aop:aspect id="innerBAspect" ref="INNER_B">
<aop:pointcut id="innerBService" expression="execution(* some.package.SomeLogic.innerB(..))"/>
</aop:aspect>

<aop:aspect id="innerBAround" ref="INNER_B">
<aop:around pointcut-ref="innerBService" method="proceed"/>
</aop:aspect>

最佳答案

是的,使用 AspectJ 很容易捕获私有(private)方法。

在所有私有(private)方法前打印一句话的例子:

 @Pointcut("execution(private * *(..))")
public void anyPrivateMethod() {}

@Before("anyPrivateMethod()")
public void beforePrivateMethod(JoinPoint jp) {
System.out.println("Before a private method...");
}

如果您熟悉 Eclipse,我建议使用 STS 开发 AspectJ或者只安装 AJDT plugin .

有关 Spring AOP 功能的更多信息可以在 Spring 引用文档中找到 here .

关于java - Aspectj 和捕获私有(private)或内部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4402009/

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