Spring的AspectJ模式缓存与AspectJ模式事务 [英] Spring's AspectJ-mode caching versus AspectJ-mode transactions
问题描述
我的问题与Spring的AspectJ模式有关,尤其是对于以下方面如何启用它:
My question relates to Spring's AspectJ mode and especially how to enable it for:
- 交易管理
- 缓存
1)我注意到,为了启用AspectJ模式进行事务管理,我只需要执行以下操作:
1) I noticed that in order to enable the AspectJ mode for transaction management, I only had to do the following:
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
2)鉴于要使用AspectJ模式进行缓存,似乎必须:
2) Whereas in order to use AspectJ mode for caching it seems one has to:
-将以下jar放入Tomcat的lib目录中: org.springframework:spring-instrument-tomcat
-在Tomcat的server.xml中添加以下行:
-Put the following jar into Tomcat's lib directory: org.springframework:spring-instrument-tomcat
-Add the following line in Tomcat's server.xml:
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
-添加以下配置:
@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class LoadTimeWeavingConfiguration implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
}
-最终能够如下使用AspectJ模式:
-to finally be able to use the AspectJ mode as follows:
@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)
以上是对的吗?如果是这样,为什么AspectJ模式缓存与AspectJ模式事务支持不同?
Is the above right? If so why is AspectJ-mode caching different from AspectJ-mode transaction support?
推荐答案
与 @EnableTransactionManagement
.如果选择 mode = AdviceMode.ASPECTJ
,则仅表示它将使用AspectJ而不是CGLIB代理进行事务管理/缓存功能.如果您启用了 spring-aspects-< version> .jar
作为方面库列出的编译时编织功能,那么它应该可以立即使用(考虑到所有其他必需的事务管理/缓存配置相关的问题)bean在应用程序上下文中可用).如果您不使用编译时编织,而是选择使用加载时编织,则在命令行上将 -javaagent:/path/to/aspectjweaver-< version> .jar
设置为JVM参数就足够了.仅当在构建中未使用编译时编织且虚拟机中不存在加载时编织代理的情况下,才需要 ReflectiveLoadTimeWeaver
和 TomcatInstrumentableClassLoader
希望通过类加载进行加载时编织.
The extra configuration you listed for the @EnableCaching
case is not any more needed than in the case of @EnableTransactionManagement
. If you choose mode = AdviceMode.ASPECTJ
it just means that it will use AspectJ instead of CGLIB proxies for the transaction management / cache functionality. If you have compile-time weaving enabled with spring-aspects-<version>.jar
listed as an aspect library, it should work out of the box (given all other required transaction management / cache configuration related beans are available in the application context). If you're not using compile-time weaving but choose instead to go with load-time weaving, having -javaagent:/path/to/aspectjweaver-<version>.jar
on the command line as a JVM argument is enough. The ReflectiveLoadTimeWeaver
and TomcatInstrumentableClassLoader
are only required in case no compile-time weaving is used in your build and a load-time weaving agent is not present in the VM, and you still want to have load-time weaving via classloading.
这篇关于Spring的AspectJ模式缓存与AspectJ模式事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!