Spring AOP self-invocation及其他问题汇总
来源:互联网 发布:社交网络爱德华多结局 编辑:程序博客网 时间:2024/05/18 07:47
spring reference中描述:
. It means that self-invocation is not going to result in the advice associated with a method invocation getting a chance to execute.
……
Finally, it must be noted that AspectJ does not have this self-invocation issue because it is not a proxybased AOP framework.
……
Method visibility and cache annotations
When using proxies, you should apply the cache annotations only to methods with public visibility. If you do annotate protected, private or package-visible methods with these annotations, no error is raised, but the annotated method does not exhibit the configured caching settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods as it changes the bytecode itself.
Tip
Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Cache* annotation, as opposed to annotating interfaces. You certainly can place the @Cache* annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies ( proxy-targetclass="true") or the weaving-based aspect ( mode="aspectj"), then the caching settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a caching proxy, which would be decidedly bad.
Note
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual caching at runtime even if the invoked method is marked with @Cacheable - considering using the aspectj mode in this case. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code, i.e. @PostConstruct.
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.
You can place the @Transactional annotation before an interface definition, a method on an interface, a class definition, or a public method on a class. However, the mere presence of the @Transactional annotation is not enough to activate the transactional behavior. The @Transactional annotation is simply metadata that can be consumed by some runtime infrastructure that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior. In the preceding example, the <tx:annotation-driven/> element switches on the transactional behavior.
Tip
Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies ( proxy-target-class="true") or the weaving-based aspect ( mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.
Note
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code, i.e. @PostConstruct.
Consider the use of AspectJ mode (see mode attribute in table below) if you expect self-invocations to be wrapped with transactions as well. In this case, there will not be a proxy in the first place; instead, the target class will be weaved (that is, its byte code will be modified) in order to turn @Transactional into runtime behavior on any kind of method.
- Spring AOP self-invocation及其他问题汇总
- spring aop缺包问题汇总
- 硬币种类问题及其他
- java 中 问题及其他
- Spring AOP 事务问题
- spring aop版本问题
- spring aop 失效问题
- Spring AOP问题
- Spring Aop相关问题
- Spring aop 一些问题
- cognos软件安装及其他问题
- [java]代码结构及其他问题
- 12球称重问题, 算法及其他
- 关于FLEX RSL及其他问题
- IO流(字符流及其他问题)
- 笔记---main函数及其他问题
- MAT文件打开方法汇总及其他操作
- 树莓派及其他硬件平台国内外Linux镜像站全汇总
- C++学习笔记一、构造函数和析构函数的概念
- vc编译设置区别
- ssh 密钥模式免输密码
- Ionic 环境配置
- DateDemo 时间格式类 SimpleDateFormat DateFormat
- Spring AOP self-invocation及其他问题汇总
- redis和memcache
- NoSQL和Redis简介及Redis在Windows下的安装和使用教程
- event.returnValue=false和return false的用处
- QT笔记:数据库总结
- DataMining学习1_数据挖掘技术(三)——关联分析
- C语言 基础练习题
- dblink名字后面有字符串REGRESS.RDBMS.DEV.US.ORACLE.COM
- Android ListView 中的Adapter 优化 缓存 getContext()