Simple Configuration of Ehcache Spring Annotations Failing

956 views
Skip to first unread message

Sarah

unread,
Sep 22, 2011, 8:20:01 AM9/22/11
to Ehcache Spring Annotations
Hi,

I'm trying to configure Ehcache Spring Annotations for a simple case
and I whenever I add the @Cacheable annotation, my web server no
longer starts, with an error creating the bean that I added the
annotation to. I'm certain that it's the @Cacheable annotation that's
causing the crash since removing it allows the web server to start
normally again.

Here are the relevant snapshots of my configuration and code:

Inside my DAO, which implements an Interface and is public:

@Cacheable(cacheName = "myCache")
public List<Long> getSqlResults(String sql) {
return this.getJdbcTemplate().queryForList(sql, Long.class);
}

In applicationContext:

<ehcache:annotation-driven />
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

In ehcache.xml
<cache name="myCache" eternal="true" maxElementsInMemory="2000"
overflowToDisk="false" diskPersistent="false" />

When I remove the @Cacheable annotation, I can see in the debug logs
that the cache myCache is created properly.:

2011-09-22 14:18:31,146 - DEBUG - [MemoryStore][main] - Initialized
net.sf.ehcache.store.LruMemoryStore for myCache

Does anyone have any idea what I've missed??

Thanks,
Sarah

Eric Dalquist

unread,
Sep 22, 2011, 8:25:00 AM9/22/11
to ehcache-sprin...@googlegroups.com
Please provide the entire stack trace from the crash caused during startup.

Sarah

unread,
Sep 22, 2011, 12:37:16 PM9/22/11
to Ehcache Spring Annotations
2011-09-22 18:36:36,052 - DEBUG - [CacheAttributeSourceImpl]
[ContainerBackgroundProcessor[StandardEngine[Catalina]]] - Adding
CACHE advised method 'getSqlResults' with attribute:
CacheableAttributeImpl
[cacheInstanceResolver=com.googlecode.ehcache.annotations.resolver.SingletonCacheableCacheResolver@169f912,
cacheKeyGenerator=HashCodeCacheKeyGenerator [includeMethod=true,
includeParameterTypes=true, useReflection=false,
checkforCycles=false], parameterMask=ParameterMask [mask=[]]]
2011-09-22 18:36:36,082 - ERROR - [ContextLoader]
[ContainerBackgroundProcessor[StandardEngine[Catalina]]] - Context
initialization failed
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'sqlCachedQueryExecutorDAOWrapper': Injection
of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private
com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAO
com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAOWrapper.sqlCachedQueryExecutorDAO;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
matching bean of type
[com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAO] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:
286) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:
1074) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:
517) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:
456) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory
$1.getObject(AbstractBeanFactory.java:291) ~[spring-
beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:
222) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:
288) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:
190) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:
580) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:
895) ~[spring-context-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:
425) ~[spring-context-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:
276) ~[spring-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:
197) ~[spring-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:
47) [spring-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:
3972) [catalina.jar:6.0.26]
at
org.apache.catalina.core.StandardContext.start(StandardContext.java:
4467) [catalina.jar:6.0.26]
at
org.apache.catalina.core.StandardContext.reload(StandardContext.java:
3228) [catalina.jar:6.0.26]
at
org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:
403) [catalina.jar:6.0.26]
at
org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:
1309) [catalina.jar:6.0.26]
at org.apache.catalina.core.ContainerBase
$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
[catalina.jar:6.0.26]
at org.apache.catalina.core.ContainerBase
$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
[catalina.jar:6.0.26]
at org.apache.catalina.core.ContainerBase
$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
[catalina.jar:6.0.26]
at org.apache.catalina.core.ContainerBase
$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
[catalina.jar:6.0.26]
at java.lang.Thread.run(Thread.java:619) [na:1.6.0_06]
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private
com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAO
com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAOWrapper.sqlCachedQueryExecutorDAO;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
matching bean of type
[com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAO] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:
507) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:
84) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:
283) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
... 23 common frames omitted
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
matching bean of type
[com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAO] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:
920) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:
789) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:
703) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:
478) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
... 25 common frames omitted


On Sep 22, 2:25 pm, Eric Dalquist <eric.dalqu...@gmail.com> wrote:
> Please provide the entire stack trace from the crash caused during startup.
>

Eric Dalquist

unread,
Sep 22, 2011, 12:41:46 PM9/22/11
to ehcache-sprin...@googlegroups.com
Is the bean you are using @Cachable in defined by an interface? If so are you injecting the interface into other beans or the implementation?

When spring applies interceptors it creates a wrapper that implements all of the interfaces of the wrapped class. That wrapper is then what is injected into other beans so your other beans can ONLY interact with the interfaces, not the implementation.

-Eric

Sarah

unread,
Sep 23, 2011, 3:54:19 AM9/23/11
to Ehcache Spring Annotations
Hi Eric,

You're right, I'm injecting the implementation, not the interface. But
that's what the sample code is doing:
http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable
"<bean id="weatherDao" class="x.y.service.DefaultWeatherDao"/>"

Cheers,
Sarah

On Sep 22, 6:41 pm, Eric Dalquist <eric.dalqu...@gmail.com> wrote:
> Is the bean you are using @Cachable in defined by an interface? If so are
> you injecting the interface into other beans or the implementation?
>
> When spring applies interceptors it creates a wrapper that implements all of
> the interfaces of the wrapped class. That wrapper is then what is injected
> into other beans so your other beans can ONLY interact with the interfaces,
> not the implementation.
>
> -Eric
>
> > {...@org.springframework.beans.factory.annotation.Autowired(required=true)}
> > {...@org.springframework.beans.factory.annotation.Autowired(required=true)}
> >        at
>
> > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
> > $AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:
> > 507) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
> >        at
>
> > org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:
> > 84) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
> >        at
>
> > org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:
> > 283) ~[spring-beans-3.0.4.RELEASE.jar:3.0.4.RELEASE]
> >        ... 23 common frames omitted
> > Caused by:
> > org.springframework.beans.factory.NoSuchBeanDefinitionException: No
> > matching bean of type
> > [com.murex.migration.totalrecon.dao.SqlCachedQueryExecutorDAO] found
> > for dependency: expected at least 1 bean which qualifies as autowire
> > candidate for this dependency. Dependency annotations:
> > {...@org.springframework.beans.factory.annotation.Autowired(required=true)}
> ...
>
> read more »

Nicholas Blair

unread,
Sep 23, 2011, 7:56:09 AM9/23/11
to ehcache-sprin...@googlegroups.com
The example you cite is correct, but is maybe missing one more piece of information.
The components that declare dependence on the WeatherDao should do so on the interface, not the implementation.

Correct:
@Controller
public class WeatherController {
@Autowired
private WeatherDao weatherDao;
...
}

Incorrect:
@Controller
public class WeatherController {
@Autowired
private DefaultWeatherDao weatherDao;
...
}

This is further demonstrated in the complete example:

https://code.google.com/p/ehcache-spring-annotations/wiki/WeatherServiceExample
Reply all
Reply to author
Forward
0 new messages