环境:Seam 2.2,ehcache-core 2.1.0

我尝试在范围为会话的bean中使用以下调用注入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
    nSource$DefaultConfigurationSource@15ed0f9.
    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.


我在这里想念什么?

最佳答案

请记住,如果要使用EhCahceProvider,则net.sf.ehcache.Cache必须位于类路径上(我不确定,但我认为ehcache-core.jar包含此类)。这是它的签名

@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属性。它的文档很清楚


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


因此,如果您的类路径包含net.sf.ehcache.Cache,则无需声明

<cache:eh-cache-provider/>


并且由于它是应用程序范围的,因此除了@ In-jection外,还可以使用

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


更新

首先


删除声明。我说你为什么(见上文)


第二


尽管我很确定CacheProvider不能为null,因为@In required属性默认情况下为true,不能为null。在您的业务方法中,确保您的CacheProvider不为null

断言cacheProvider!= null


三分之一


我认为您不需要调用cacheProvider.instance()方法。如果其默认范围是Application。为什么要检索另一个CacheProvider?它没有任何意义。


第四


这也不例外。这只是一个警告消息,因为您尝试使用多个缓存提供程序,而两者都使用相同的内存空间

07-27 23:21