1
votes

We use EhCacheManagerFactoryBean which has stopped working after upgrading spring framework versions from 4.1.6 to 4.3.27. I get BeanCreationException at application startup.

I tried by overriding the springframework dependencies to 4.1.6, and it worked. It also worked for 4.2.5 and 4.2.8. Fails for 4.3.27 and 4.3.5 (haven't tried other 4.3.x versions).

Here are my relevant pom dependencies

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-core</artifactId>
                <version>2.6.7</version>
        </dependency>

I could isolate the problem being relevant for spring-context, spring-aop, and spring-context-support (downgraded these to 4.2.5 and kept rest at 4.3.27 - it worked.)

cache bean definition - the cacheManager bean is used as reference to define other beans,

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd 
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true" />

<bean id="abstractCache" class="<class that extends org.springframework.cache.ehcache.EhCacheFactoryBean>" p:cache-manager-ref="cacheManager" abstract="true" />
....

</beans>

Exception, Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in file

stacktrace, Caused by: java.lang.IllegalStateException: Singleton instance not initialized yet at org.springframework.util.Assert.state(Assert.java:70) at org.springframework.beans.factory.config.AbstractFactoryBean.getSingletonInstance(AbstractFactoryBean.java:180) at org.springframework.beans.factory.config.AbstractFactoryBean.access$200(AbstractFactoryBean.java:61) at org.springframework.beans.factory.config.AbstractFactoryBean$EarlySingletonInvocationHandler.invoke(AbstractFactoryBean.java:265) at com.sun.proxy.$Proxy20.iterator(Unknown Source) at org.springframework.cache.ehcache.EhCacheFactoryBean.afterPropertiesSet(EhCacheFactoryBean.java:291) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1677) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1615)

Appreciate any help. Thank you.

1
post your full xml.Anish B.
@Anish B. Updated the config xml that shows the usage of "cacheManager" bean. I am thinking I might need to make some changes to use EhCacheCacheManager as per your answer. Thoughts?Vinayak

1 Answers

0
votes

I had this xml configuration for Spring 4.3.14.RELEASE and Ehcache 2.10.6 below :

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

    
    <!-- Added cache annotation support -->
    <cache:annotation-driven
        cache-manager="ehCacheCacheManager" />

    <!-- Added cache manager -->
    <bean id="ehCacheCacheManager"
        class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cacheManager-ref="ehCacheManagerFactoryBean" />

    <!-- Added cache manager factory -->
    <bean id="ehCacheManagerFactoryBean"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:configLocation="classpath:ehcache.xml" p:shared="true" />

</beans>

It should work.