我想在我的Spring Boot 4应用程序中添加缓存。 作为一个键,我想使用我的实体的id(类型Long),value是我的实体。
@EnableCaching
@SpringBootApplication
public class SpringBootMainApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplication.class, args);
}
}
@Configuration
public class AppConfiguration {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("findOne");
}
}
public interface SampleEntityService extends CrudRepository<SampleEntity, Long> {
@Cacheable(key = "#a0", value = "findOne")
SampleEntity findOne(Long id);
但是当我在测试中调用findOne方法时,我得到了一个例外:
java.lang.ClassCastException: org.springframework.cache.support.SimpleValueWrapper cannot be cast to my.springboot.model.SampleEntity
at my.springboot.SampleEntityCacheTest.aaa(SampleEntityCacheTest.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
根据一些教程,我尝试使用许多变体,但所有变体都失败了。
@Cacheable(key = "#a0", value = "findOne")
@Cacheable(key = "#p0", value = "findOne")
@Cacheable(key = "a0", value = "findOne")
@Cacheable(key = "p0", value = "findOne")
@Cacheable(key = "#id", value = "findOne")
@Cacheable(key = "id", value = "findOne")
@Cacheable(key = "#root.id", value = "findOne")
@Cacheable(key = "root.id", value = "findOne")
我做错了什么?
=======回答=========
正确的解决方案是:
@Cacheable(key = "#a0", value = "findOne")
在测试中我应该做的就是这样:
@Test
public void aaa() {
SampleEntity entity = sampleEntityService.save(create("name1", uuid(1)));
sampleEntityService.findOne(entity.getId());
SampleEntity entityFromCache =(SampleEntity) cacheManager.getCache("findOne").get(entity.getId()).get();
assertNotNull(entityFromCache);
}