Spring aop拦截来自同一服务类的调用 [英] Spring aop intercepting calls from within the same service class

查看:235
本文介绍了Spring aop拦截来自同一服务类的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与

Spring @Transaction method call by the method within the same class, does not work?

我指的是答案#1,我认为这对我的简单pojo课有效,但它没有。在我的情况下,我没有注释@Transaction。它是一个简单的pojo类。如果我在上面的帖子中举例,我希望拦截每个方法adduser以及使用spring aop的addusers。

I was referring to answer #1 which i thought would work for my simple pojo class but it didnt. In my case i dont have annotation @Transaction. Its a simple pojo class. I wish to intercept each method adduser as well as addusers using spring aop if i take example in post above.

是否可以截取从同一服务调用中调用的方法?我指的是AspectJAwareProxy,它可以解决问题,但不能解决整个问题。我的意思是我不希望任何东西被添加到我的业务逻辑中。所以我想避免任何编码,除了定义切入点和定义建议。是否有可能使用Java和spring aop?我正在使用CGlib来生成代理。 Spring版本是3.0.5。发布。

Is it possible to intercept method that is being called from the same service call? I was referring to AspectJAwareProxy which does the trick but doesnt resolve the issue as a whole. What i mean is i dont want anything to be added to my business logic. So i want to avoid any coding except for defining pointcut and defining the advice. Is it something possible using Java and spring aop? I am using CGlib to generate proxy. Spring version is 3.0.5.Release.

谢谢,
Ajay

Thanks, Ajay

推荐答案

为此,您必须使用加载时编织而不是代理。原因是因为spring使用代理来实现AOP功能(例如事务支持)。一旦进入类实例,对同一实例中的方法的任何方法调用都将直接针对实际的实例对象,而不是包装代理,因此不会考虑AOP建议。加载时编织的工作方式不同。在那里,你有一个外部java代理,它在字节码级别操作类来注入AOP支持。

For this to work, you have to use load-time weaving instead of proxying. The reason is because spring uses proxying to achieve AOP functionality (such as transaction support). Once inside a class instance, any method-calls to methods in the same instance will be directly against the actual instance object, not the wrapping proxy so AOP advices will not be considered. Load-time weaving works differently. There, you have an outside java agent that manipulates the classes on a byte-code level to inject the AOP support.

你需要

1:修改用于包含spring aspectj代理的java命令行

1: Modify your java command-line used to include the spring aspectj agent

2:add

<context:load-time-weaver aspectj-weaving="on" />
<tx:annotation-driven mode="aspectj" />

到春季cofig。

阅读更多:

使用Spring Transaction Manager和Maven构建AspectJ加载时间

http://static.springsource.org/spring/docs/3.0.x/ spring-framework-reference / html / aop.html#aop-aj-ltw

这篇关于Spring aop拦截来自同一服务类的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆