title: spring - forcing cglib proxies for @Autowired fields
body: <p>I have mixed stack: EJB and Spring. For autowiring Spring to EJBs I use <code>SpringBeanAutowiringInterceptor</code> (not sure if that could affect the problem I have).</p>
  
  <p>Whlie trying to autowire beans in a following way:</p>
  
  <pre><code>@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="singleton")
  @Repository
  public class ClassA imlpements IfA {
      ...
  }
  
  
  @Scope(value="singleton")
  @Repository
  public class ClassB {
      @Autowired
      private ClassA classA;
  
      ...
  }
  </code></pre>
  
  <p>I get error:</p>
  
  <pre><code>Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257. Please see server.log for more details.
  </code></pre>
  
  <p>As far as I've understood, the problem is that JDK proxies are used (as <code>ClassA</code> implements interface <code>IfA</code>), while I'd need cglib ones.</p>
  
  <p>But I don't get it. Based on the docs, the annotation: <code>@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS,</code> should do the job here.</p>
  
  <p>Well and it's not an option for me to use interfaces (as a field type). </p>
  
  <p>Any idea how to force cglib proxying here?</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Not sure if it's relevant, but I'm running glassfish 3.1.x. </p>
  
  <p>And stacktrace (partial, as I can't expose all the class names here:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classB': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257.
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
      ... 89 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257. Please see server.log for more details.
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
      ... 100 more
  Caused by: java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
      at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
      at java.lang.reflect.Field.set(Field.java:741)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:510)
      ... 102 more
  </code></pre>