bal*_*teo 6 aspectj spring-aop spring-data-jpa spring-cache
我的问题与Spring的AspectJ模式有关,特别是如何启用它:
1)我注意到为了启用AspectJ模式进行事务管理,我只需要执行以下操作:
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
Run Code Online (Sandbox Code Playgroud)
2)为了使用AspectJ模式进行缓存,似乎必须:
- 将以下jar放入Tomcat的lib目录: - org.springframework:spring-instrument-tomcat
在Tomcat的server.xml中添加以下行:
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
Run Code Online (Sandbox Code Playgroud)
- 添加以下配置:
@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class LoadTimeWeavingConfiguration implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
}
Run Code Online (Sandbox Code Playgroud)
- 最终能够使用AspectJ模式如下:
@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)
Run Code Online (Sandbox Code Playgroud)
以上是对的吗?如果是这样,为什么AspectJ模式缓存与AspectJ模式事务支持不同?
您为该@EnableCaching案例列出的额外配置并不比 的情况更需要@EnableTransactionManagement。如果您选择mode = AdviceMode.ASPECTJ它,则意味着它将使用 AspectJ 而不是 CGLIB 代理来实现事务管理/缓存功能。如果您启用了编译时编织并列spring-aspects-<version>.jar为方面库,那么它应该可以开箱即用(假设所有其他所需的事务管理/缓存配置相关的 bean 在应用程序上下文中可用)。如果您不使用编译时编织,而是选择使用加载时编织,那么-javaagent:/path/to/aspectjweaver-<version>.jar在命令行上作为 JVM 参数就足够了。仅当您的构建中未使用编译时织入且虚拟机中不存在加载时织入代理且您仍希望通过类加载进行加载时织入时,才需要ReflectiveLoadTimeWeaver和。TomcatInstrumentableClassLoader
| 归档时间: |
|
| 查看次数: |
1141 次 |
| 最近记录: |