无法解析对bean' cacheManager'的引用

时间:2015-04-04 04:41:48

标签: java xml spring junit ehcache

尝试在我的Spring应用程序中对Controller运行单元测试。

我的测试班:

@RunWith (SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration ({"classpath*:WEB-INF/spring/application-context-config.xml", "data-source-test-config.xml", 
                        "classpath*:WEB-INF/spring/security-context-config.xml"})
public class PropsControllerTest 

我得到的错误:

2015-04-03 23:22:51.729 WARN  AbstractApplicationContext:487 - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined

我的application-context-config.xml定义了:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
    <property name="shared" value="true"/>
</bean>

ehcache.xml定义:

<ehcache>
    <cache name="backslap"
           maxBytesLocalHeap="256M"
           eternal="false"
           timeToLiveSeconds="3600"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>
</ehcache>

我已经在三个地方获得了ehcache.xml的副本:src / main / resources /,test / main / resources /以及我的测试类的目录。没有什么能解决这个问题。

如何让Spring在测试期间找到文件(运行时查找文件没有问题)?如果做不到这一点,我如何让Spring停止想要这些信息(缓存在我的测试中完全没用),或者至少让我在application-context-config.xml文件中指定它?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

错误:

 No bean named 'cacheManager' is defined

此错误是由于Spring无法识别此bean。

确定定义此bean的文件,并确保它可用于测试上下文。

相关问题