Sunday, May 31, 2015

org.springframework.a op.framework.AopConfigException: Could not generate CGLIB subclass of class [class net.sf.ehcache.CacheManager]

org.springframework.a op.framework.AopConfigException: Could not generate CGLIB subclass of class [class net.sf.ehcache.CacheManager]

While using Ehcache with spring, spring context initialization fails with the below exception

Caused by: org.springframework.beans.factory.BeanCreationException: Error creati
ng bean with name 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceI
mpl#0': Cannot resolve reference to bean 'ehcacheManager' while setting bean pro
perty 'cacheManager'; nested exception is org.springframework.beans.factory.Bean
CreationException: Error creating bean with name 'ehcacheManager': Post-processi
ng of the FactoryBean's object failed; nested exception is org.springframework.a
op.framework.AopConfigException: Could not generate CGLIB subclass of class [cla
ss net.sf.ehcache.CacheManager]: Common causes of this problem include using a f
inal class or a non-visible class; nested exception is org.springframework.cglib
.core.CodeGenerationException: net.sf.ehcache.CacheException-->Another unnamed C
acheManager 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 Cac
heManager 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 [stre
am=java.io.BufferedInputStream@128647a]

Configuration Details:

<?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: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://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
     http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven cache-manager="cacheManager"/>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="configLocation" value="classpath:config/ehcache/ehcache.xml" />
  <property name="shared" value="true" />
</bean>
</beans>


ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
    <!-- Location of persistent caches on disk -->
    <diskStore path="java.io.tmpdir/FizzhubCache" />
    <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>      
        <!--
    <cache name="weatherCache" eternal="false"
        maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
        timeToIdleSeconds="0" timeToLiveSeconds="300"
        memoryStoreEvictionPolicy="LRU" />
    -->
</ehcache>

Dependencies:

 <dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
      </dependency>
   
      <dependency>
  <groupId>com.googlecode.ehcache-spring-annotations</groupId>
  <artifactId>ehcache-spring-annotations</artifactId>
  <version>1.2.0</version>
</dependency>

After analysis the issue is due to the AOP proxy is enabled in the project, spring fails  to create the AOP proxy for net.sf.ehcache.CacheManager and intern the context initialization is failing.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- Adding the Intercepter - ThrowsAdvice to handle exceptions on system -->
<bean id="exceptionHandlerAdvice" class="com.fizzhub.framework.aop.ExceptionHandlerAdvice" >
</bean>

<!-- Applying the advice to the objects in the system -->
<bean id="systemExceptionHandlerBeanFactoryProxyCreator"
          class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name="beanNames">
<list>
<value>*Dao</value>
<value>*DAO</value>
<value>*Service</value>
<value>*Manager</value>
<value>*Controller</value>
<value>*Impl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>exceptionHandlerAdvice</value>
</list>
</property>
</bean>
</beans>

After removing the *Manager from the list, the spring context initialized successfully.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- Adding the Intercepter - ThrowsAdvice to handle exceptions on system -->
<bean id="exceptionHandlerAdvice" class="com.fizzhub.framework.aop.ExceptionHandlerAdvice" >
</bean>

<!-- Applying the advice to the objects in the system -->
<bean id="systemExceptionHandlerBeanFactoryProxyCreator"
          class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name="beanNames">
<list>
<value>*Dao</value>
<value>*DAO</value>
<value>*Service</value>
<value>*Controller</value>
<value>*Impl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>exceptionHandlerAdvice</value>
</list>
</property>
</bean>
</beans>


No comments:

Post a Comment