0

我想在我的 Spring 应用程序中使用 Spring AOP。在创建 AOP 代理时net.sf.ehcache.CacheManager,spring 上下文初始化失败,出现以下异常:

nested exception is org.springframework.aop.framework.AopConfigException: Could not
generate CGLIB subclass of class [class net.sf.ehcache.CacheManager]: Common causes of 
this problem include using a final class or a non-visible class; nested exception is 
net.sf.cglib.core.CodeGenerationException: net.sf.ehcache.CacheException-->Another   
unnamed CacheManager already exists in the same VM. Please provide unique names for 
each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same  
CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource 
[stream=java.io.ByteArrayInputStream@955b34]

这是我对这个问题的理解 - Spring 正在尝试为net.sf.ehcache.CacheManager.__DEFAULT__应用)。现在,如果我有多个缓存管理器,例如 'abcCacheManager' 和 'xyzCacheManager' (类型为 EhCacheManagerFactoryBean),Spring 会遇到多个net.sf.ehcache.CacheManagers 并尝试为所有这些管理器创建代理对象(类似于net.sf.ehcache.CacheManager$$EnhancerByCGLIB$$b18c5958),但是使用 EhCache >=2.5 版本,我们同一虚拟机下不能有多个同名缓存。

我正在使用 EhCache 2.5.1,并希望避免仅出于此目的回到 2.4。我不确定这是否真的是我如何克服这个问题的问题。

注意:请注意这是否会有所帮助,但我也从调试语句中注意到CacheManager无参数构造函数仅由 spring/CGLIB 代理生成器xyzCacheManager调用,并通过将配置作为参数传递来调用它。

4

1 回答 1

0

注意:我自己回答这个问题,因为它可能会帮助面临同样问题的其他人。

jeha对我的问题的评论是有道理的,因为我一开始就不应该需要那个代理,但是由于我是 Spring AOP 和代理的新手,我不知道自动代理机制是如何工作的。由于我修改了建议中的切入点表达式,之后我没有遇到上述问题。在此之前,几乎容器中的所有 bean 都被代理了,因此出现了问题。

于 2012-09-12T09:05:07.073 回答