繁体   English   中英

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

[英]Autowired spring bean null for cacheManager using ehcache spring annotations

我对Spring还是很陌生,我正在尝试正确设置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
}...}

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

查看Spring缓存文档 ,您需要声明另一个bean作为您从工厂创建的CacheManager

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM