将我们的 spring 升级到版本 4 后,我们的自定义密钥生成器停止工作。在迁移之前,我们执行了覆盖“generate”方法的代码,但是迁移到spring 4.0.5之后,代码根本没有执行。相反,我看到 SimpleKeyGenerator 是始终执行的那个。这是春天的虫子吗?为什么我不能像以前的版本那样用自己的代码覆盖 generate 方法?
来自根上下文的样本:
<cache:annotation-driven key-generator="cacheKeyGenerator" />
<bean id="cacheKeyGenerator" class="com.poalim.xp.general.cache.CacheKeyGenerator"/>
来自 java 密钥生成的示例(迁移之前)
public class CacheKeyGenerator extends DefaultKeyGenerator implements ApplicationContextAware {
public Object generate(Object target, Method method, Object... params) {
return method.getName() + super.generate(target, method, params);
}
}
迁移后的示例代码
public class CacheKeyGenerator extends SimpleKeyGenerator implements ApplicationContextAware {
public Object generate(Object target, Method method, Object... params) {
return method.getName() + super.generate(target, method, params);
}
}
附加信息:调试代码后,我看到每次调用“生成”方法时,它仅在 SimpleKeyGenerator 中执行,而不在我的自定义 CacheKeyGenerator 类中执行。我试图理解为什么,所以我做了一些调试。在调试时,我看到有 org.springframework.cache.interceptor.CacheAspectSupport 类,它有一个私有属性: private KeyGenerator keyGenerator = new SimpleKeyGenerator(); 这个类在 keyGenerator 属性上有一个 setter 方法,我看到当上下文启动时,这个 setter 方法是用我自定义的 CacheKeyGenerator 调用的,所以我断定我的配置是正确的,问题不在配置中。我还看到,当需要生成密钥时,keyGenerator 属性“丢失”了“CacheKeyGenerator”值并具有“