如何围绕实现 java.security.Principal 的类中的方法创建切面?
有谁知道如何在实现接口 java.security.Principal 的类中围绕方法“getName()”创建方面?
我正在使用 spring,下面是我的类的相关部分:
@Aspect
public class MyPrincipalAspect {
@PointCut("(execution(* java.security.Principal.getName(..)))")
private void getNamePC(){}
@Around("getNamePC()")
public Object getNameJP(ProceedingJoinPoint pjp) throws Throwable {
Object retVal = pjp.proceed();
return retVal;
}
}
我实际上想用“retVal”做一些事情,但是,我只是使用上面的内容作为一个简单的例子。我的应用程序中有其他方面,它们都工作得很好。
我在 AspectJ 网站上读到一些关于无法从 java 包加载时编织类的内容,但他们说这仍然是可能的(但是,他们没有给出任何示例)。
我还尝试过非 Spring 方法(使用 aop.xml 文件和在我的应用程序服务器配置参数中定义的 javaagent)。
非常感谢任何帮助。
谢谢。
Does anyone know how one would go about creating an aspect around the method "getName()" in a class that implements the interface java.security.Principal?
I am using spring and below is the pertinent parts of my Class:
@Aspect
public class MyPrincipalAspect {
@PointCut("(execution(* java.security.Principal.getName(..)))")
private void getNamePC(){}
@Around("getNamePC()")
public Object getNameJP(ProceedingJoinPoint pjp) throws Throwable {
Object retVal = pjp.proceed();
return retVal;
}
}
I actually want to do something with "retVal", however, I'm just using the above as a trivial example. I have other Aspects within my application and they all work just fine.
I read something on the AspectJ site regarding not being able to load-time weave classes from the java package, but they say it's still possible (but, they don't give any examples).
I've also tried going through a non-Spring approach (using an aop.xml file with javaagent defined in my app-server config params).
Any help is much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有多种选择:
getName
的客户端类,则可以使用call
切入点而不是execution
。这样你就不需要编织任何 JDK 类。Principal
子类不是JDK,而是第三方类(你没有提到),你可以在编译或加载时轻松编织它们。更新:哦,顺便说一句,在你的切入点中,你可能想要使用
Principal+
而不仅仅是Principal
,以便也拦截重写的方法。You have several options:
getName
, you can use acall
pointcut instead ofexecution
. This way you do not need to weave any JDK classes.Principal
subclasses you want to intercept are not JDK, but third-party classes (you did not mention), you can weave them easily at compile or load time.Update: Oh by the way, in your pointcut you might want to use
Principal+
instead of justPrincipal
in order to also intercept overridden methods.