使用带Spring缓存的构造函数参数:注释驱动
将缓存添加到应用程序上下文时,我收到以下异常:注释驱动的:使用带Spring缓存的构造函数参数:注释驱动,spring,spring-annotations,Spring,Spring Annotations,将缓存添加到应用程序上下文时,我收到以下异常:注释驱动的: Superclass has no null constructors but no arguments were given 我有一个名为PersistController的类,它的方法用@Cachable(..)注释,我在应用程序上下文中声明一个实例,如下所示: <!-- The culprit, for whatever reason --> <cache:annotation-driven /> &l
Superclass has no null constructors but no arguments were given
我有一个名为PersistController的类,它的方法用@Cachable(..)注释,我在应用程序上下文中声明一个实例,如下所示:
<!-- The culprit, for whatever reason -->
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="primaryTest"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="secondaryTest"/>
</set>
</property>
</bean>
<!-- The problem began when I started requiring a String in the constructor. -->
<bean id="persistController" class="com.sei.poc.controller.PersistController" >
<constructor-arg> <value>${cache.nextVersionPostfix}</value></constructor-arg>
</bean>
${cache.nextVersionPostfix}
当我删除cache:annotation-driven行时,错误消失了,相反,我收到一个错误,指示带注释的缓存组件不工作(显然)。这意味着上面声明的PersistController已正确声明,Spring完全能够调用正确的构造函数、解析属性并将字符串作为参数注入
那么,为什么在添加“缓存:注释驱动”时出错
我相信这与Spring自动实例化具有@Cacheable(..)或任何Spring缓存注释的类实例有关。如果是这种情况,我想知道是否有一种方法可以将我的属性指定为其构造中的参数
如果我不能为这些类声明构造函数,(如果您知道答案,我会假设您比我更了解spring上下文流),那么@Autowired或@Value(…)注释是否允许生成的类检测属性
感谢大家阅读。我找到了问题第二部分的答案,即当spring使用缓存加载类时,是否可以解析带有的@Value()注释 答案是肯定的,我在我的PersistController中添加了以下行,问题已经解决:
@Value("${cache.currentVersionPostfix}")
private String keyPostfix;
我仍然不确定是否可以在加载了缓存:注释驱动的的构造函数中提供参数。话虽如此,我将暂缓接受这个答案,并接受一个回答这个问题的答案