Problems with ehcache:annotation-driven and spring 3.0.5 component scan

720 views
Skip to first unread message

Jonas

unread,
Mar 23, 2011, 7:08:50 AM3/23/11
to Ehcache Spring Annotations
Hey guys,

I have a problem configuring Spring 3.0.5 with ehcache:annotation
driven.

If I configure the ehcache, Autowireing of all beans including a
@Cacheable Annotation fails. If I remove the Annotation OR the
Configuration in the applicationContext the project works fine again.
Here are some snippets of my configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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
">

<context:component-scan base-package="de.myproject" />
<ehcache:annotation-driven cache-manager="ehCacheManager" />

[...]

<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="shared" value="false"/>
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>


and the failing class:


@Service
public class ConfigurationServiceImpl implements ConfigurationService
{
[...]

@Override
@Cacheable(cacheName = "professionList")
public LinkedList<String> getProfessionListByTerm(String language,
String term, String gender) {
[...]
}
}

I am using Spring 3.0.5.RELEASE with

<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.1.3</version>
</dependency>

and


<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.3.2</version>
<type>pom</type>
</dependency>

Has anyone an idear whats going wrong here?

Thank you!

Eric Dalquist

unread,
Mar 23, 2011, 7:24:56 AM3/23/11
to ehcache-sprin...@googlegroups.com
Can you include one of your @Autowired setters that fails to be called?

-Eric

Jonas Lanzendörfer

unread,
Mar 23, 2011, 7:29:00 AM3/23/11
to ehcache-sprin...@googlegroups.com

@Component
public class AutocompleteProfessionMethodProcessor implements MethodProcessor {

@Autowired
ConfigurationServiceImpl configurationService;
[...]

Eric Dalquist

unread,
Mar 23, 2011, 7:37:46 AM3/23/11
to ehcache-sprin...@googlegroups.com
You need to inject the interface, not the implementation. @Cacheable works just like any other Spring AOP proxy where a wrapper class is generated that implements all of the interfaces of the target class. Because of this you cannot cast the wrapped class to its actual implementation, you must use the interface that defines the class's APIs to interact with it.

So change your autowiring to:

@Autowired
ConfigurationService configurationService


-Eric

Jonas Lanzendörfer

unread,
Mar 23, 2011, 7:49:51 AM3/23/11
to ehcache-sprin...@googlegroups.com
Oh great, didn´t know that. Saved me a lot of time, thank you so much!

Eric Dalquist

unread,
Mar 23, 2011, 7:52:57 AM3/23/11
to ehcache-sprin...@googlegroups.com
No problem, in general when working with Spring beans should only ever depend on other interfaces. That is how Spring assumes the application will be designed and the only way you really get a loosely coupled application.

-Eric
Reply all
Reply to author
Forward
0 new messages