@Cachable注释不起作用 [英] @Cachable annotation does not work

查看:119
本文介绍了@Cachable注释不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在项目中使用ehcache进行缓存。

We're using ehcache for caching purposes in our project.

import com.googlecode.ehcache.annotations.Cacheable;
// Other imports

@Component
public class Authenticator{
    @Cacheable(cacheName = "rest_client_authorized")
    public boolean isUserAuthorized(final String user, final String sessionId) {
        // Method code
    }
}

输入方法时,没有缓存拦截器。到目前为止,我们检查的内容:

When entering the method there is no cache interceptor. The things we checked so far:


  1. 我们不是从类内部调用此方法,而是从外部调用此方法。因此,问题不在于导致绕过代理的内部调用。

  2. 我们为该类添加了一个接口,并且我们更改了调用该类的注入以使用接口表示形式

我们已经通过以下方式在应用程序上下文中定义了缓存管理器:

We have defined the cache manager in our application context this way:

   <ehcache:annotation-driven cache-manager="ehCacheManager" />         
   <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
       <!-- use a share singleton CacheManager -->
       <property name="shared" value="true" />
   </bean>

缓存的定义如下:

        <cache name="rest_client_authorized"
            eternal="false"
            maxElementsInMemory="50"
            overflowToDisk="false" diskPersistent="false"
            timeToIdleSeconds="0" timeToLiveSeconds="600"
            memoryStoreEvictionPolicy="LRU" />

当我们使用Jconsole测试缓存管理器时,我们可以看到缓存* rest_auth_disabled *存在并且为空。

When we test the cache manager using Jconsole we can see that the cache *rest_auth_disabled* exists and empty.

关于为什么这样做不起作用的任何想法都将受到赞赏。谢谢!

Any ideas as to why this does not work will be most appreciated. Thanks!

更新(来自以下评论的汇总)

=== ===================================== **

==========================================**

这是一个旧代码,可以很好地与我提供的类和定义一起使用。我在这里谈论的方法是新的,但该课程的其余部分过去确实有用。因此,我们正在努力了解发生了什么变化。我们也已经尝试过将注解替换为spring Cacheable,但是仍然没有什么:/
也许取决于调用此新方法的代码,该代码来自与其他方法不同的spring bean。但是我仍然找不到问题。

That's a legacy code that worked fine with the class and definitions I've provided. The method I talk about here is new, but the rest of the class did work in past. So we're struggling to understand what has changed. We also tried already to replace the annotation to spring Cacheable, but still nothing :/ Maybe that depends on the code that calls this new method, which is from a different spring bean than what we used for the other methods. But I still can't find the issue.

也尝试按照下面的答案返回布尔值而不是布尔值,但这没有用。
我们有一个新线索,可能与我们注入bean的方式有关(使用@Autowire)。

Also tried to return Boolean instead of boolean following an answer below and it didn't work. We have a new lead which is probably related to the way we inject the bean (using @Autowire). Will update if that's indeed the case.

推荐答案

此问题可能与Springs加载bean的顺序有关。尝试从Authenticator声明中删除@Autowire批注,然后手动进行自动装配。

This problem might have to do with the order Springs loads the beans. Try removing the @Autowire annotation from the Authenticator declartion, and do the autowiring manually. Something like:

/**
 * Class that uses Authenticator
 */

 public class X {

    // Don't use @autowire here
    public Authenticator athenticator;

    public void doSomething() {
         manuallyAutowire();
    }

    public void manuallyAutowire() {
         if(authenticator == null) {
    authenticator = ApplicationContextUtils.getApplicationContext().
                            getBean(authenticator.class);
    }
}

在哪里

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext ctx;

    @Override
    public void setApplicationContext(final ApplicationContext appContext) 
                                          throws BeansException {
         ctx = appContext;

    }

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }
}

这篇关于@Cachable注释不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆