简体   繁体   English

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

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

I'm still new to spring and I'm trying to get ehcache spring annotations setup correctly. 我对Spring还是很陌生,我正在尝试正确设置ehcache spring注释。 I'm using Spring 3.2.3 ehCache 2.4 and ehcache-spring-annotations-1.2. 我正在使用Spring 3.2.3 ehCache 2.4和ehcache-spring-annotations-1.2。

When I try to access the reference to the cacheManager, it is always null. 当我尝试访问对cacheManager的引用时,它始终为null。 All the jars are on the build path, ehcache.xml is in the classpath and there are no xml errors. 所有的jar都在构建路径上,ehcache.xml在类路径中,并且没有xml错误。 I've tried also including the classes in the component scan and using @Resource instead of Autowired. 我也尝试过在组件扫描中包括这些类,并使用@Resource代替Autowired。 I'm stuck! 我被卡住了!

Application context: 应用环境:

<?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>

Wrapper 包装纸

@Component
public final class MyCache implements Serializable {

@Autowired
private CacheManager ehCacheManager;

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

It seems you are trying to use the EhCacheManagerFactoryBean as your cache manager. 看来您正在尝试使用EhCacheManagerFactoryBean作为缓存管理器。

Looking at Spring caching documentation , you need to declare another bean to be your CacheManager created from the factory. 查看Spring缓存文档 ,您需要声明另一个bean作为您从工厂创建的CacheManager

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

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