English 中文(简体)
与继承的点混淆
原标题:Pointcut confusion with inheritance

写一个与所有执行方法匹配的指针, 使我感到困惑。 我尝试了一个指针, 它应该匹配所有方法执行 类 < code> Alpha 的 方法 :

execution(* Alpha.*(..))

与以下类级高频相交

public class Alpha {
    public void alphaMethod() {...}
}
public class Beta extends Alpha {
    public void betaMethod() {
        alphaMethod();
    }
}

如果主程序程序在 Beta /code>上调用 alphaMethod < /code> 来调用一个 Beta 来调用我的建议,我的建议会被调用,但主程序电话 betaMethod 也没有被调用,我不明白为什么。

频谱定义 :

@Aspect
public class MyAspect {
    @Before(value = "execution(* Alpha.*(..))", argNames="joinPoint")
    public void myAdvice(JoinPoint joinPoint) {
        System.out.println("BEFORE: " + joinPoint.getSignature());
    }
}

主方法 :

Beta beta = ...;
beta.alphaMethod(); //advice is called
beta.betaMethod(); //advice is NOT called.
最佳回答

预计会这样。

Spring AOP 使用代理类包装建议豆类。当您在 Beta 方法中调用 alphaMethod () 时,代理甚至不知道它的存在。

回答更多信息。

问题回答

原因已经回答过,原因已经很清楚。 您可以使用 < em> load-time 编织 来解决这个问题, 而不是代理( 我想可以归到某处的配置选项和对AspectJ的依附性, 如果尚未依此而定的话), 或者将豆作为依赖性输入给自己, 然后执行

public void betaMethod() {
    selfBean.alphaMethod();
}

这将有效, 因为 selfBean this 不相同, 前者是代理的引用, 后者是原始的、 代理对象的引用 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签