2010-06-16 67 views
3

ENV:缝2.2,ehcache的核心2.1.0无法注入煤层缓存提供

我试图用我的豆以下调用注入的CacheProvider范围的会话

@In CacheProvider cacheProvider; 

    WEB-INF\components.xml contains the following line to enable the cache provider 
    <cache:eh-cache-provider/> 

The above configuration seems to return a null value for the cache provider 


Using the cache provider like this   
CacheProvider cacheProvider = CacheProvider.instance(); 
throws the following warning 

    15:29:27,586 WARN [CacheManager] Creating a new instance of CacheManager using 
    the diskStorePath "C:\DOCUME~1\user5\LOCALS~1\Temp\" which is already used by an 
    existing CacheManager. 
    The source of the configuration was net.sf.ehcache.config.generator.Configuratio 
    [email protected] 
    The diskStore path for this CacheManager will be set to C:\DOCUME~1\user5\LOCALS 
    ~1\Temp\\ehcache_auto_created_1276682367586. 
    To avoid this warning consider using the CacheManager factory methods to create 
    a singleton CacheManager or specifying a separate ehcache configuration (ehcache 
    .xml) for each CacheManager instance. 

我在想什么这里?

+0

我使用的Ehcache-core.jar添加我还需要ehcache.jar在一条警告消息lib文件夹? – Joe 2010-06-16 17:07:47

+0

只是为了好奇:你有没有曾经使用过一些Cache提供者?你说你的@注入组件**似乎返回null **它不能返回null,因为来自@In注解的必需属性默认情况下是true **,这意味着它不能为空**。否则,你会得到一个由Seam抛出的异常。你遵循哪个教程? – 2010-06-19 20:34:32

+0

它似乎没有引发异常,cacheProvider只是null。查询缓存和二级缓存似乎工作正常,虽然 – Joe 2010-06-22 07:30:16

回答

1

请记住net.sf.ehcache.Cache 需要在类路径(我不确定,但我认为ehcache-core.jar包含此类)如果您要使用EhCahceProvider。这里有它的签名

@Name("org.jboss.seam.cache.cacheProvider") 
@Scope(APPLICATION) 
@BypassInterceptors 
@Install(value = false, precedence=BUILT_IN, classDependencies="net.sf.ehcache.Cache") 
@AutoCreate 
public class EhCacheProvider extends CacheProvider<CacheManager> { 

注意classDependencies属性。它的文档清晰

指示该组件不应安装除非在给定的类定义可用的类路径上

所以,如果你的classpath包含net.sf. ehcache.Cache 你不需要申报

<cache:eh-cache-provider/> 

而且因为它是应用范围的,你可以在-jection,使用的所有

  • ApplicationContext.getContext().get("cacheProvider"); 
    

    UPDATE

    首先拆下<缓存中检索,除了@:EH- cache-provider/>声明。我说你为什么(见上文)

二的所有

  • 虽然我敢肯定的CacheProvider 不能为空因为@In所需的属性,默认情况下,真实的,哪些不能为空。里面的业务方法,确保您的CacheProvider不为空

    断言的cacheProvider!= NULL

三的所有

  • 我想你不需要调用cacheProvider.instance( )方法。如果其默认范围是应用程序。 为什么你想要检索另一个CacheProvider?这没有意义。

四的所有

  • 它不是一个例外。它仅仅是因为你试图使用一个以上的高速缓存提供商那里都使用相同的内存空间
+0

net.sf.ehcache.Cache是​​ehcache-core.jar的一部分,并且存在于耳\ lib文件夹 我曾尝试以下方法和没有似乎工作 //的CacheProvider的cacheProvider = CacheProvider.instance(); 或 的CacheProvider的cacheProvider =(的CacheProvider)上下文 .getApplicationContext()得到。 (“cacheProvider”); 或 @In CacheProvider cacheProvider; – Joe 2010-06-22 07:29:40

+0

@Joshua确保war lib文件夹一旦存储在ear lib文件夹中就不包含ehcache-core.jar – 2010-06-23 12:15:52

+0

该jar仅存在于ear \ lib文件夹 – Joe 2010-06-23 17:52:25