Java 如何在Spring3中执行方法后强制@Cacheable工作?
我有一个将实体保存到数据库的方法。它是:Java 如何在Spring3中执行方法后强制@Cacheable工作?,java,spring-cache,Java,Spring Cache,我有一个将实体保存到数据库的方法。它是: @Cacheable(value = EMPLOYEE_CACHE,key="#employee.id") public Employee createEmployee(Employee employee) { try { entityManager.persist(employee); } catch (Exception e) { return null;
@Cacheable(value = EMPLOYEE_CACHE,key="#employee.id")
public Employee createEmployee(Employee employee) {
try {
entityManager.persist(employee);
} catch (Exception e) {
return null;
}
return employee;
}
我希望通过Employee
的key=id
缓存结果Employee
。员工的id
在运行entityManager,persist
方法后分配。
我知道由于AOP,@Cacheable
可以工作。默认情况下,在方法createEmployee
运行之前,它将employee
存储到缓存中
如何强制org.springframework.cache.annotation.@Cacheable
在方法执行后存储到缓存中
堆栈跟踪:
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) CachePutOperation[public com.livelessons.spring.springcore.entities.Employee com.livelessons.spring.springcore.service.EmployeeServiceImpl.createEmployee(com.livelessons.spring.springcore.entities.Employee)] caches=[employees] | key='#employee.id' | condition='' | unless=''
at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheUpdates(CacheAspectSupport.java:371)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:202)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy30.createEmployee(Unknown Source)
at com.livelessons.spring.springcore.EmployeeServiceTest.saveEmployee(EmployeeServiceTest.java:59)
at com.livelessons.spring.springcore.EmployeeServiceTest.testSpringCacheable(EmployeeServiceTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
您可以定义自己的注释,例如@DefferedCachable,然后使用aop以编程方式在缓存中存储/读取对象。您应该使用
@CachePut
而不是@Cacheable
(具有相同的属性):
与{@Cacheable}注释相反
此注释不会导致跳过目标方法
相反,它总是导致调用方法并将其结果放入缓存中
有点晚了,但可能对某人有用。 @Cacheable从方法参数定义键,而不是返回值,因此,如果传递的是没有id的新Employee对象,则会得到null。 简单的解决方案可能如下所示:
public Employee createEmployeeWithCaching(Employee employee) {
Employee employee = persistEmployee(employee);
return saveEmployeeInCache(employee);
}
private Employee persistEmployee(Employee employee) {
try {
return entityManager.persist(employee);
} catch (Exception e) {
return null;
}
}
@Cacheable(value = EMPLOYEE_CACHE,key="#employee.id")
private Employee saveEmployeeInCache(Employee employee) {
return employee;
}
但是,如果确实需要在
catch
块中返回null
,请三思。显然,它将导致saveEmployeeInCache
方法中的其他异常。我在文档和我的代码@CachePut中将实体存储到缓存中,然后再执行方法。我在调用@CachePut方法时尝试了一个不为null的属性,它存储OK,但如果我将null属性id
用作CachePut
的key
,则会引发异常。我甚至在@CachePut方法中显式地分配了id,并得到了相同的异常。请尝试调用entityManager.flush()
调用persist()
之后。它将为员工对象分配一个id。谢谢!这没有帮助,因为@CachePut在方法调用之前将存储放入缓存。查看stacktrace的编辑。