【发布时间】:2014-10-07 23:59:50
【问题描述】:
我有混合堆栈:EJB 和 Spring。对于将 Spring 自动装配到 EJB,我使用 SpringBeanAutowiringInterceptor(不确定这是否会影响我遇到的问题)。
尝试通过以下方式自动装配 bean:
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="singleton")
@Repository
public class ClassA imlpements IfA {
...
}
@Scope(value="singleton")
@Repository
public class ClassB {
@Autowired
private ClassA classA;
...
}
我得到错误:
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.
据我了解,问题在于使用了 JDK 代理(因为 ClassA 实现了接口 IfA),而我需要 cglib 代理。
但我不明白。根据文档,注释:@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, 应该在这里完成工作。
好吧,我不能选择使用接口(作为字段类型)。
知道如何在此处强制 cglib 代理吗?
编辑:
不确定是否相关,但我正在运行 glassfish 3.1.x。
还有stacktrace(部分,因为我不能在这里公开所有的类名:
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
【问题讨论】:
-
你能发布一个完整的堆栈跟踪吗?
-
你在使用 spring-data-jpa 作为存储库吗?
-
spring-data-jpa?没有