2

当我将cache:annotation-driven添加到我的应用程序上下文时,我收到以下异常:

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:annotation-driven行时,错误消失了,而是收到一个错误,表明带注释的缓存组件不工作(显然)。这意味着上面声明的PersistController已正确声明,并且 Spring 完全能够调用正确的构造函数、解析属性并将字符串作为参数注入。

那么为什么我添加cache:annotation-driven时出现错误?

我相信这与 Spring 使用@Cacheable(..)或任何 Spring 缓存注释自动实例化类的实例有关。如果是这种情况,我想知道是否有一种方法可以将我的属性指定为其构造中的参数。

如果我不能为这些类声明构造函数,(如果你知道答案,我会假设你比我更了解 spring-context-flow)将@Autowired@Value(..)注释允许生成的类检测财产?

谢谢大家的阅读。

4

1 回答 1

0

我已经找到了问题第二部分的答案,即当 spring 使用cache:annotation-driven加载类时是否解决了@Value()注释。

答案是肯定的,我在PersistController中添加了以下行,问题已解决:

@Value("${cache.currentVersionPostfix}")
private String keyPostfix;

我仍然不确定是否可以在加载了cache:annotation-driven的构造函数中提供参数。话虽如此,我将推迟接受这个作为答案,并接受一个回答这个问题的答案。

于 2013-03-25T12:04:59.797 回答