我正在使用注释在现有的Spring项目上添加spring。我使用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缓存?我错过了什么或者做错了什么?
我在Spring4.3.5上。
更新-
下面是我复制情况的最简单的代码- https://github.com/harshilsharma63/spring-boilerplate-with-cache
发布于 2017-01-03 23:59:12
忘记基于类加载器的加载时编织,尤其是Tomcat < 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...
,@EnableAspectJAutoProxy
中删除未嵌套的配置,如context.xml
、@EnableAspectJAutoProxy
。当然,最好还是只使用编译时编织。
https://stackoverflow.com/questions/41373745
复制相似问题