使用ehcache spring注释为cacheManager自动装配spring bean null

时间:2015-03-25 16:27:10

标签: java spring annotations code-injection ehcache

我还是刚接触到春天,我正在尝试正确设置ehcache spring注释。我正在使用Spring 3.2.3 ehCache 2.4和ehcache-spring-annotations-1.2。

当我尝试访问对cacheManager的引用时,它始终为null。所有jar都在构建路径上,ehcache.xml在类路径中,没有xml错误。我也试过在组件扫描中包含类并使用@Resource而不是Autowired。我被卡住了!

申请背景:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
ehcache-spring-1.1.xsd">

 <context:component-scan      base-package="org.springframework.cache.ehcache.EhCacheManagerFactoryBean,com    .defaultPackage,net.sf.ehcache.CacheManager" />

<!-- ehCache Annotation settings -->
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager"   class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml" />
    <property name="shared" value="true"/>
</bean>

包装

@Component
public final class MyCache implements Serializable {

@Autowired
private CacheManager ehCacheManager;

private getCacheManager() {
    return ehCacheManger; // this is always null
}...}

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试使用EhCacheManagerFactoryBean作为缓存管理器。

查看Spring caching documentation,您需要将另一个bean声明为从工厂创建的CacheManager