【发布时间】:2014-07-07 14:52:14
【问题描述】:
将 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”值并具有“SimpleKeyGenerator”。 这解释了为什么我的自定义代码从未执行,但我不明白为什么 keyGenerator 属性指向 SimpleKeyGenerator。 这似乎是一个弹簧 BUG。 有什么问题?
【问题讨论】:
标签: spring spring-cache