【问题标题】:Autowired spring bean null for cacheManager using ehcache spring annotations使用ehcache spring注解为cacheManager自动装配spring bean null
【发布时间】:2015-03-25 16:27:10
【问题描述】:

我还是 spring 新手,我正在尝试正确设置 ehcache spring 注释。我正在使用 Spring 3.2.3 ehCache 2.4 和 ehcache-spring-annotations-1.2。

当我尝试访问对 cacheManager 的引用时,它始终为空。所有 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
}...}

【问题讨论】:

    标签: java spring annotations code-injection ehcache


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2014-06-30
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多