我正在使用注释在现有的spring项目上添加Spring Cache。我正在使用Couchbase作为缓存提供程序。我想使用AspectJ进行加载时编织,以允许私有方法调用和case类方法调用也被缓存。
我被困在这个问题已经有三天了,我已经阅读了几十篇文章,文档和例子,但这件事情无效。
这就是我所做的 -
@Configuration
@EnableSpringConfigured
@EnableAspectJAutoProxy
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
@EnableTransactionManagement
@EnableRetry
@PropertySource(
value = {"classpath:application.properties", "classpath:${spring.profiles.active}.properties"},
ignoreResourceNotFound = true)
public class BeanConfig implements LoadTimeWeavingConfigurer {
... various beans here ...
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new TomcatLoadTimeWeaver();// because I'm using Tomcat 7
}
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
return new InstrumentationLoadTimeWeaver();
}
}
@Configuration
@EnableSpringConfigured
@EnableCaching(mode = AdviceMode.ASPECTJ)
@ComponentScan(basePackages = "com.foo.bar.dao.cache.couchbase")
public class CacheConfigurer extends CachingConfigurerSupport {
@Bean
@Override
public CacheManager cacheManager() {
... cachemanager configuration here ...
}
}
然后我在类上的DAO方法上有@Chacheable
,而不是在接口上。
最后,在我的Tomcat 7的$ CATALINA_HOME / conf / context.xml中我有 -
<Context>
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
我在pom.xml(它是一个maven项目)中添加了以下依赖项 -
如果我不使用LTW缓存,那么通过接口到达的方法调用就可以正常工作(就像它应该的那样)。但是在我启用LTW后,缓存根本不起作用 - 没有任何方法调用的缓存,也没有错误。
有没有人尝试过使用LTW进行带有couchbase的Spring缓存?我在这里错过了什么或做错了什么?
我在春季4.3.5。发布。
更新 -
这是我复制情况的极小代码 - https://github.com/harshilsharma63/spring-boilerplate-with-cache
答案 0 :(得分:1)
忘记基于类加载器的加载时编织,特别是对于Tomcat&lt; 8.0。你会遇到很多与类加载顺序相关的问题,有些类在spring安装他的编织类加载器之前被加载,你最终会遇到难以调试的问题,你的一些类没有被编织等等。相反,请使用java代理。
以下是如何通过切换到基于java代理的编织来修复Tomcat 7的配置:
@EnableLoadTimeWeaving
注释。<Loader loaderClass...
移除context.xml
。-javaagent:/path/to/aspectjweaver.jar
添加到JVM启动参数中。如果您愿意转移到Tomcat 8,请执行以下步骤:
@EnableLoadTimeWeaving(aspectjWeaving=ENABLED)
移至单独的配置类,我们将其命名为WeavingConfig
。WebInitializer
课程,以便getRootConfigClasses()
仅返回WeavingConfig.class
。getServletConfigClasses()
。<Loader loaderClass...
,context.xml
移除@EnableAspectJAutoProxy
等不需要的配置。当然,最好的还是只使用编译时编织。