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

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

问题描述

我有一个与

Spring同一个类内的方法调用@Transaction方法,不行吗?

我指的是答案 #1,我认为它适用于我的简单 pojo 课程,但它没有.就我而言,我没有注释@Transaction.它是一个简单的 pojo 类.如果我在上面的帖子中举例,我希望使用 spring aop 拦截每个方法 adduser 和 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.Release.

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.

谢谢,阿杰

推荐答案

为此,您必须使用加载时编织而不是代理.原因是因为 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:添加

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

到你的弹簧配置.

阅读更多:

AspectJ 加载时间编织与 Spring 事务管理器和马文

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

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

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