title: Error creating bean implementing ApplicationListener with @Cacheable method
body: <p>We've been having issues while deploying our application, and, after a while of research, we've found the problem source, but we don't know why is it happening.</p>
  
  <p>The application tries to wire a bean 'bar' on a bean 'foo' on Spring context initialization. 'Bar' bean implements interface ApplicationListener, and has a @Cacheable method as well. For some reason we don't know, there's a crash on bean creation. Removing @Cacheable or making the bean not being an instance of ApplicationListener makes the app running OK.</p>
  
  <p>Maybe is a misconception of ApplicationListener and/or @Cacheable, but what's the reason of this error to occur? Why is 'bar' bean 'illegal'? Spring version is 3.2.9.</p>
  
  <p>Spring context file</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:cache="http://www.springframework.org/schema/cache"
     xmlns:task="http://www.springframework.org/schema/task"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache.xsd
     http://www.springframework.org/schema/task
     http://www.springframework.org/schema/task/spring-task.xsd"&gt;
  
  &lt;bean id="applicationContextProvder" class="com.mycompany.ApplicationContextProvider"/&gt;
  
  &lt;!-- CACHE --&gt;
  &lt;cache:annotation-driven cache-manager="springEhCacheManager"/&gt;
  
  &lt;bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt;
      &lt;property name="configLocation" value="/WEB-INF/conf/ehcache.xml"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="springEhCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"&gt;
      &lt;constructor-arg ref="ehCacheManager"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="cacheProvider" class="org.springmodules.cache.provider.ehcache.EhCacheFacade"&gt;
      &lt;property name="cacheManager" ref="ehCacheManager"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="cacheService" class="com.myapp.cache.DefaultEHCacheServiceImp"&gt;
      &lt;constructor-arg index="0" ref="ehCacheManager"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- ALIVE --&gt;
  &lt;bean id="voldemortAlive"
        class="com.myapp.voldemort.VoldemortAliveImp"&gt;
  &lt;/bean&gt;
  
  &lt;bean id="atlasAlive"
        class="com.myapp.atlas.AtlasAliveImp"&gt;
  &lt;/bean&gt;
  
  
  
  &lt;task:annotation-driven executor="myExecutor" scheduler="myScheduler"/&gt;
  &lt;task:executor id="myExecutor" pool-size="5"/&gt;
  &lt;task:scheduler id="myScheduler" pool-size="10"/&gt;
  
  &lt;!-- custom beans defined --&gt;
  
  &lt;bean id="updatersSynchronizedTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"/&gt;
  &lt;bean id="updatersSynchronizedTaskExporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>Foo class</p>
  
  <pre><code>package com.foo
  
  @Service
  public class Foo {
  
      @Autowired
      private Bar bar;
  
      ... lots of stuff here ...
  
      @PostConstruct
      protected void init() {
           ... method body ...
      }
  
  ... more logic ...
  </code></pre>
  
  <p>}</p>
  
  <p>Bar class</p>
  
  <pre><code>package com.bar
  
  @Repository
  public class Bar extends BarParent implements ApplicationListener&lt;BarEvent&gt;   {
  
  ... more stuff ...
  
      @Cacheable("cache_name")
      public List&lt;Baz&gt; cachedMethod(final Integer code) {
          ... some logic here ...
      }
  ...
  }
  </code></pre>
  
  <p>Exception given</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bar.Bar com.foo.Foo.bar; nested exception is java.lang.IllegalArgumentException: Can not set com.bar.Bar field com.foo.Foo.bar to $Proxy86
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
  ... 63 more
  Caused by: java.lang.IllegalArgumentException: Can not set com.bar.Bar field com.foo.Foo.bar to $Proxy86
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
  at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
  at java.lang.reflect.Field.set(Field.java:657)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
  ... 65 more
  </code></pre>
  
  <p>Thank you in advance.</p>