spring 无法为CacheableOperation[]缓存找到名为“”的缓存

qaxu7uf2  于 2022-11-21  发布在  Spring
关注(0)|答案(9)|浏览(153)

我的错误是:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot find cache named 'getActionsBycasId' for CacheableOperation[public java.util.List com.codinko.database.DataBaseConnection.getActionsByCasId(int)] caches=[getActionsBycasId] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''
    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:81)
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:214)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:553)
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:227)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:498)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.codinko.database.DataBaseConnection$$EnhancerBySpringCGLIB$$21a0d8a.getActionsByCasId(<generated>)
    at com.codinko.caching.EmployeeDAO.getActionBycasId(EmployeeDAO.java:47)
    at com.codinko.caching.EmployeeDAO$$FastClassBySpringCGLIB$$191aa49b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
    at com.codinko.caching.EmployeeDAO$$EnhancerBySpringCGLIB$$3399d753.getActionBycasId(<generated>)
    at com.codinko.caching.Main.main(Main.java:22)

我的职能是:

@Cacheable("getActionsBycasId")
public List<SMSAction> getActionsByCasId(int casId){
    System.out.println("Inside getActionsByCasId");
    //My logic
    return list;
}

当我在ehcache.xml上添加以下内容时,上面的错误不会出现,但不知道为什么会出现此错误。

<cache name="getActionsBycasId" maxElementsInMemory="50" eternal="false"
    overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />

即使我使用了annotation,ehcache.xml文件中是否需要上述配置?

y4ekin9u

y4ekin9u1#

试试看:

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    List<Cache> caches = new ArrayList<Cache>();
    caches.add(new ConcurrentMapCache("getActionsBycasId"));
    cacheManager.setCaches(caches);
    return cacheManager;
}
hm2xizp9

hm2xizp92#

如果使用spring cloud aws,请禁用自动弹性缓存配置。

@EnableAutoConfiguration(exclude = ElastiCacheAutoConfiguration.class)
@EnableCaching
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
pvcm50d1

pvcm50d13#

@SpringBootApplication(exclude = {
        ContextStackAutoConfiguration.class,
        ElastiCacheAutoConfiguration.class
})

只是吹毛求疵,使用**@SpringBootApplication而不是@启用自动配置**

v440hwme

v440hwme4#

试试换

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="cacheManagerName" value="cacheManager_service" />
  <property name="configLocation" value="classpath:ehcache-services.xml" />
  <property name="shared" value="true" />
</bean>

结束日期

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" primary="true">
  <property name="cacheManagerName" value="cacheManager_service" />
  <property name="configLocation" value="classpath:ehcache-services.xml" />
</bean>
wribegjk

wribegjk5#

您可以在resources文件夹中定义ehcache.xml,并考虑配置、、标记和设置别名作为您的缓存方法。

w80xi6nr

w80xi6nr6#

添加ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
    <config xmlns='http://www.ehcache.org/v3'>

        <persistence directory="${java.io.tmpdir}" />

        <!-- Default cache template -->
        <cache-template name="default">
            <expiry>
                <tti unit="hours">4</tti>
                <!-- <ttl unit="minutes">2</ttl> -->
            </expiry>
            <listeners>
                <listener>
                    <class>com.org.lob.support.LoggingTaskCacheListener</class>
                    <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                    <event-ordering-mode>UNORDERED</event-ordering-mode>
                    <events-to-fire-on>CREATED</events-to-fire-on>
                    <events-to-fire-on>EXPIRED</events-to-fire-on>
                    <events-to-fire-on>REMOVED</events-to-fire-on>
                    <events-to-fire-on>UPDATED</events-to-fire-on>
                </listener>
            </listeners>
            <resources>
                <heap unit="MB">10</heap>
                <offheap unit="MB">50</offheap>
                <disk persistent="true" unit="GB">1</disk>
            </resources>
            <!-- 
            <heap-store-settings>
                <max-object-graph-size>2000</max-object-graph-size>
                <max-object-size unit="kB">5</max-object-size>
            </heap-store-settings>
            -->
        </cache-template>

        <!-- Cache configurations -->
        <cache alias="books" uses-template="default" >
            <key-type>java.lang.String</key-type>
            <value-type>com.org.lob.project.repository.entity.Book</value-type>     
        </cache>

        <cache alias="files" uses-template="default" >
            <key-type>java.lang.String</key-type>
            <value-type>java.lang.String</value-type>       
        </cache>

    </config>

更新您的application.properties

spring.cache.jcache.config=classpath:ehcache.xml
cld4siwp

cld4siwp7#

由于类路径资源中的ehcache.xml文件已经具有配置,因此最好的方法是使用此文件配置初始化Bean:

@Bean
  public EhCacheManagerFactoryBean ehCacheCacheManager() {
    final EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
    ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
    ehCacheManagerFactoryBean.setShared(true);
    return ehCacheManagerFactoryBean;
  }

  @Bean
  public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheCacheManager().getObject());
  }
pgky5nke

pgky5nke8#

只需在单元测试的配置中创建一个如下所示的@Bean:

@Bean
public CacheManager cacheManager() {
  return new ConcurrentMapCacheManager("cacheName1", "cacheName2", "cacheName3", "cacheNameN");
}
n3h0vuf2

n3h0vuf29#

上面的排除选项,对我不起作用。但是,下面的起作用了。!!如果你使用的是Spring aws cloud,那么就像下面一样排除弹性。

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-messaging</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.amazonaws</groupId>
                    <artifactId>
                        elasticache-java-cluster-client
                    </artifactId>
                </exclusion>
            </exclusions>
        </dependency>

相关问题