簡體   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