使用LoadTimeWeaving的Couchbase Spring缓存 - 奇怪的不工作

时间:2016-12-29 05:42:10

标签: java spring aspectj spring-cache load-time-weaving

我正在使用注释在现有的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项目)中添加了以下依赖项 -

  • couchbase - 弹簧 - 高速缓存
  • 弹簧方面
  • aspectjweaver
  • aspectjrt
  • spring-instrument
  • 弹簧仪器的Tomcat

如果我不使用LTW缓存,那么通过接口到达的方法调用就可以正常工作(就像它应该的那样)。但是在我启用LTW后,缓存根本不起作用 - 没有任何方法调用的缓存,也没有错误。

有没有人尝试过使用LTW进行带有couchbase的Spring缓存?我在这里错过了什么或做错了什么?

我在春季4.3.5。发布。

更新 -

这是我复制情况的极小代码 - https://github.com/harshilsharma63/spring-boilerplate-with-cache

1 个答案:

答案 0 :(得分:1)

忘记基于类加载器的加载时编织,特别是对于Tomcat&lt; 8.0。你会遇到很多与类加载顺序相关的问题,有些类在spring安装他的编织类加载器之前被加载,你最终会遇到难以调试的问题,你的一些类没有被编织等等。相反,请使用java代理。

以下是如何通过切换到基于java代理的编织来修复Tomcat 7的配置:

  1. 删除@EnableLoadTimeWeaving注释。
  2. <Loader loaderClass...移除context.xml
  3. -javaagent:/path/to/aspectjweaver.jar添加到JVM启动参数中。
  4. 如果您愿意转移到Tomcat 8,请执行以下步骤:

    1. @EnableLoadTimeWeaving(aspectjWeaving=ENABLED)移至单独的配置类,我们将其命名为WeavingConfig
    2. 更改您的WebInitializer课程,以便getRootConfigClasses()仅返回WeavingConfig.class
    3. 将其他所需的配置类移至getServletConfigClasses()
    4. <Loader loaderClass...context.xml移除@EnableAspectJAutoProxy等不需要的配置。
    5. 利润。
    6. 当然,最好的还是只使用编译时编织。