FactoryBean和自定义范围的bean

时间:2013-06-03 15:37:36

标签: spring scope

我正在使用自定义FactoryBean来构建一组单例对象。从FactoryBean我以编程方式调用'autowireBeanProperties'将依赖项注入bean。

context.getAutowireCapableBeanFactory().autowireBeanProperties(o, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);

(我知道这不是一个最佳方案,但我有理由以这种方式设计东西,我打算改变,但我认为需要更多时间)

其中一个依赖项是自定义范围的bean,我们将其称为“userSession”范围。

我收到以下错误:

Property 'scopedBean' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.connector': Scope 'userSession' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: Unknown identity: null

我尝试了两个和一个自定义的ScopedProxy(仅在方法调用时实例化内部userSession-scoped对象),但结果是相同的。 堆栈看起来像这样:

UserSessionScope.get(String, ObjectFactory) line: 23    
DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 329    
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 194   
SimpleBeanTargetSource.getTarget() line: 33 
JdkDynamicAopProxy.invoke(Object, Method, Object[]) line: 184   
$Proxy0.setScopedBean(IScopedBean) line: not available  
MySingletonBean.setScopedBean(IScopedBean) line: 51
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
Method.invoke(Object, Object...) line: 597  
BeanWrapperImpl.setPropertyValue(BeanWrapperImpl$PropertyTokenHolder, PropertyValue) line: 1134 
BeanWrapperImpl.setPropertyValue(PropertyValue) line: 904   
BeanWrapperImpl(AbstractPropertyAccessor).setPropertyValues(PropertyValues, boolean, boolean) line: 75  
BeanWrapperImpl(AbstractPropertyAccessor).setPropertyValues(PropertyValues) line: 57    
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyPropertyValues(String, BeanDefinition, BeanWrapper, PropertyValues) line: 1424  
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1132 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).autowireBeanProperties(Object, int, boolean) line: 379   
MyFactoryBean.processInjection(Object) line: 68

我无法想象为什么在实例化阶段调用了调用... 我没有逻辑可以做到这一点......

另一件事是:我没有名为'scopedTarget.connector'的bean。

感谢您提供任何线索

(顺便说一句:我使用的是Spring 3.2版本)

1 个答案:

答案 0 :(得分:0)

好的,这是我的错。我想知道为什么我会得到那种不相关的堆栈跟踪...

问题出在其他地方......