我坚持这个问题。
二手组件:Spring 1.2.8,Hibernate 3.2.0 cr1,tomcat,struts,java 6
我正在尝试使用scope = prototype从ProxyFactoryBean获取bean。我没有成功。我不知道怎么了。
这里是上下文:
<beans>
<bean id="ruleCheckTask" class="rulechecker.RuleCheckTask" singleton="false">
<bean id="ruleCheckTaskPrototype" class="org.springframework.aop.target.PrototypeTargetSource">
<property name="targetBeanName" value="ruleCheckTask" />
</bean>
<bean id="transactionInterceptorRuleCheckTask" class="org.springframework.transaction.interceptor .TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<value>
rulechecker.IRuleCheckTask.run=PROPAGATION_REQUIRE S_NEW
</value>
</property>
</bean>
<bean id="ruleCheckTaskService" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="target" ref="ruleCheckTaskPrototype" />
<property name="proxyInterfaces">
<value>
rulechecker.IRuleCheckTask
</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptorRuleCheckTask</value>
</list>
</property>
</bean>
</beans>
在代码中,当我执行以下操作时:
..........
..........
IRuleCheckTask checkTask =(IRuleCheckTask)applicationContext.getBean(“ ruleCheckTaskService”);
checkTask.setTestCase(oneTestCase);
尝试在checkTask bean上调用setTestCase时出现以下异常:
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:181)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :148)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:96)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :170)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy21.setTestCase(Unknown Source)
如果在ProxyFactoryBean中我使用ruleCheckTask而不是ruleCheckTaskPrototype,则可以正常工作。问题在于,在这种情况下,我总是获得单例的RuleCheckTask。而且我总是需要新实例。
RuleCheckTask的一件小事实现了Runnable接口。
有人可以给我一个提示吗?
谢谢
最佳答案
尝试:
<bean id="ruleCheckTaskService" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="targetName" value="ruleCheckTask" />
<property name="singleton" value="false" /> <!-- this do the trick -->
<property name="proxyInterfaces">
<value>
rulechecker.IRuleCheckTask
</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptorRuleCheckTask</value>
</list>
</property>
</bean>
您也可以将
targetSource
(没有target
)设置为ruleCheckTaskPrototype
。不同之处在于,在第一个实例上,您具有代理配置的独立实例,在第二个实例上,PrototypeTargetSource
在每个请求上获取一个新实例。