Announcement Announcement Module
Collapse
No announcement yet.
How to get prototype from ProxyFactoryBean ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get prototype from ProxyFactoryBean ?

    Hello all,

    first of all I would like to say I am a beginner in spring, so maybe I am asking basic thing, but I am really stuck in my problem.

    Used components: Spring 1.2.8, Hibernate 3.2.0 cr1, tomcat, struts, java 6

    I am trying to get bean from ProxyFactoryBean with scope = prototype. I am not successful. I have no clue what is wrong.

    Here is context:

    <beans>

    <bean id="ruleCheckTask" class="rulechecker.RuleCheckTask" singleton="false">

    <bean id="ruleCheckTaskPrototype" class="org.springframework.aop.target.PrototypeTar getSource">
    <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>


    In code when I do following:

    ...................
    ...................
    IRuleCheckTask checkTask = (IRuleCheckTask) applicationContext.getBean("ruleCheckTaskService") ;
    checkTask.setTestCase(oneTestCase);

    I got following exception when trying call setTestCase on checkTask bean:

    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)

    It was working if in ProxyFactoryBean I used ruleCheckTask instead ruleCheckTaskPrototype. Problem is that in that case I always obtain singleton of ruleCheckTask. And I need always new instance.
    One small thing RuleCheckTask implements Runnable interface.

    Could anybody give me a hint?

    Thank you
    Last edited by flanker; Nov 16th, 2012, 03:55 PM.

  • #2
    Here is the solution

    <bean id="ruleCheckTaskService" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="targetName" value="ruleCheckTask" />
    <property name="singleton" value="false" />
    <property name="proxyInterfaces">
    <value>
    rulechecker.IRuleCheckTask
    </value>
    </property>
    <property name="interceptorNames">
    <list>
    <value>transactionInterceptorRuleCheckTask</value>
    </list>
    </property>
    </bean>


    I was making 2 basic mistakes. I put singleton property directory into declaration <bean singleton=false>, and then I got exception, that ProxyFactoryBean has to be singleton. And second mistake from my side was, I specified target through property "target" and "ref" instead of "targetName" and "value". What a small details and they are important.

    Comment

    Working...
    X