我的 @Around 建议不会被包中的所有方法调用

发布于 2024-09-11 14:54:14 字数 519 浏览 0 评论 0原文

我的 LoggingAspect 类中有下面的代码,我希望它可以为我的方法运行,例如

gov.ssa.rome.service.impl.save() gov.ssa.rome.dao.impl.save()

但无论如何它只运行一次。我不知道为什么。我已经使用 autowire 将 dao 连接到 service 层。我真的很感谢你的帮助。

我应该怎么做才能让这个方法对我的所有应用程序流运行以查看日志中的流?

@Around("execution(* gov.ssa.rome..*.*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable {

 System.out.println("aspect Around started");

        Object ret = pjp.proceed();

    System.out.println("aspect Around ended);

  return ret;
}

I have this code below in my LoggingAspect class and i am expect this to run for my methods like

gov.ssa.rome.service.impl.save()
gov.ssa.rome.dao.impl.save()

but it is running only one time no matter what. i don't know why. i have used autowire to wire dao to servcice layer. I really appreciate your help.

what should i do to make this method run for all my application flow to see the flow in logs?

@Around("execution(* gov.ssa.rome..*.*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable {

 System.out.println("aspect Around started");

        Object ret = pjp.proceed();

    System.out.println("aspect Around ended);

  return ret;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-09-18 14:54:14

可以使用不同的技术来创建方面。如果您的代理是 JDK 代理,它们将仅适用于接口中定义的方法。如果它们是 cglib 代理,它们将适用于除最终方法之外的所有方法。我认为如果匹配的类实现了一个接口,spring 默认会使用 JDK 代理,否则使用 cglib 代理。

检查带有 save 方法的类是什么样子以及 save 是否来自接口。您可以强制使用aspectjweaver代理来使一切正常工作,但它们需要一些字节码操作。我建议坚持使用 JDK 代理并在需要时创建/扩展接口。有关更多信息,请参阅 spring 文档 AOP 章节。

Aspects can be created using different technologies. If yours are JDK proxies, they will work only for methods defined in an interface. If they are cglib proxies, they will work for all but final methods. I think spring by default will use JDK proxy if matching class implements an interface and cglib proxy otherwise.

Check what your class with save method looks like and whether save comes from an interface. You can enforce aspectjweaver proxies to make everything work, but they require some bytecode manipulation. I would recommend sticking to JDK proxies and creating/extending interfaces where needed. Refer to spring documentation AOP chapter for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文