通过调用bean内的方法来调用代理AOP

发布于 2024-09-16 10:33:11 字数 319 浏览 1 评论 0原文

可以说我有一个用两种方法“foo”和“goo”调用的bean 而'goo'则标记有AOP拦截调用。
是否可以在“foo”内编写任何代码段,以便不直接调用“goo”方法,而是通过bean的代理包装器来激活它的AOP部分?

public Class Pojo{

  public void foo(){
    //what should I write here in order to activate 'goo' in transactional mode??
  }

  @Transactional
  public void goo(){
  }
}

lets say the I have got a bean called with two methods 'foo' and 'goo'
and 'goo' is marked with AOP interception call.
is it possible to write any piece of code inside 'foo' in order to invoke 'goo' method not directly but through the proxy wrapper of the bean in order to activate the AOP part of it?

public Class Pojo{

  public void foo(){
    //what should I write here in order to activate 'goo' in transactional mode??
  }

  @Transactional
  public void goo(){
  }
}

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

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

发布评论

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

评论(2

一刻暧昧 2024-09-23 10:33:11

可以,但是需要通过spring代理来访问:

public Class Pojo{

  @Autowired
  private Pojo springProxy;

  public void foo(){
    springProxy.goo();
  }

  @Transactional
  public void goo(){
  }
}

Yes, but you need to access it through the spring proxy:

public Class Pojo{

  @Autowired
  private Pojo springProxy;

  public void foo(){
    springProxy.goo();
  }

  @Transactional
  public void goo(){
  }
}
表情可笑 2024-09-23 10:33:11

我无法使用自动连线选项。也许是因为我使用反射从 foo() 调用 goo() (以及任何其他方法)。
所以最终解决我的问题的是添加 foo() 代码来查找 Pojo 的代理 bean 类。并在代理 bean 上使用 Sun 调用来调用 mothd
这也调用了 AOP 调用。
找不到更好的解决方法。

I couldn't use the autowired option. Perhaps it is because I am using reflection to invoke goo() (and any other method as well) from foo().
So eventually what solves my problem was to add in foo() code that will lookup for the Pojo's proxy bean class. and invoke the mothd using Sun invokation on the proxy bean
this invoked the AOP call as well.
Couldn't find any better workaround.

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