2012-09-09 36 views
0

我想在我的spring應用程序中使用Spring AOP。在創建AOP代理爲net.sf.ehcache.CacheManager,Spring上下文初始化失敗,出現以下異常:在Ehcache CacheManager中使用CGLIB代理

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 
[[email protected]] 

這裏是我對這個問題的認識 - 春天試圖創建AOP代理爲net.sf.ehcache.CacheManager,並且取得了成功的第一時間,並給出了默認CacheManager __DEFAULT__的名稱(通過向ehcache代碼添加調試語句,通過源代碼構建並在我的應用程序中使用它)找到此名稱。現在,如果我有多個緩存管理器(如'abcCacheManager'和'xyzCacheManager'(類型爲EhCacheManagerFactoryBean)),Spring會遇到多個net.sf.ehcache.CacheManager s,並嘗試爲它們創建代理對象(類似於net.sf.ehcache.CacheManager$$EnhancerByCGLIB$$b18c5958),但EhCache> = 2.5版本,我們在同一個虛擬機下不能有多個同名的緩存。

我正在使用EhCache 2.5.1,並希望避免只爲此目的回到2.4。我不確定這是否真的是我如何克服這個問題的問題。注意:請注意,如果這會有所幫助,但我也從調試語句中注意到,CacheManager no-arg構造函數僅由spring/CGLIB代理生成器調用,xyzCacheManager通過將配置作爲參數傳遞來調用它。

+1

你真的需要'CacheManager'的CGLIB代理嗎? – jeha

回答

0

注:我自己回答,因爲它可能會幫助其他面臨同樣問題的人。關於我的問題

jeha的評論是有道理的,因爲我不應該需要一個代理在首位,但由於我是新來的Spring AOP和代理,我不知道如何自動代理機制的作品。當我在我的建議中修改了切入點表達式之後,在那之後我沒有遇到上述問題。在此之前,容器中幾乎所有的bean都被代理了,因此出現了問題。