gpt4 book ai didi

java - 如何在 spring 3 方法执行后强制 @Cacheable 工作?

转载 作者:行者123 更新时间:2023-11-29 05:22:10 25 4
gpt4 key购买 nike

我有将实体保存到数据库的方法。它是:

@Cacheable(value = EMPLOYEE_CACHE,key="#employee.id")
public Employee createEmployee(Employee employee) {
try {
entityManager.persist(employee);
} catch (Exception e) {
return null;
}
return employee;
}

我希望结果 EmployeeEmployee 的键 = id 缓存。员工的 identityManager,persist 方法运行后分配。我知道 @Cacheable 可以工作是因为 AOP。默认情况下,它会在方法 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)

最佳答案

您可以定义自己的注释,例如@defferedCacheable,然后使用aop以编程方式存储/读取缓存中的对象。

关于java - 如何在 spring 3 方法执行后强制 @Cacheable 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24251733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com