|
From: MacMahon, M. <M.M...@em...> - 2003-11-24 12:01:30
|
Hi All,
The problem that I described in my previous email appears to be fixed in
the 1.0 M3 (prepared) release!
Thanks again,
Mark.
-----Original Message-----
From: MacMahon, Mark [mailto:M.M...@em...]
Sent: 21 November 2003 14:58
To: 'spr...@li...'
Subject: RE: [Springframework-developer] Intercepting a Prototype Bean
Hi Rod, all,
Thanks very much for the prompt and detailed replies.
I have tried to apply your suggested solution to my problem but am still
encountering the same issues
I.E the same TestTarget instance is being re-used for each invocation!
Have I misunderstood?
I have defined the following in applicationContext.xml
<!-- TestTarget implements the ITestTarget interface - one method foo() -->
<bean id="TestTarget" class="test.TestTarget" singleton="false">
</bean>
<!-- Transaction Interceptor Chain-->
<!-- Specified as a prototype (for mixin behaviour) -->
<bean id="MixinTransactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor"
singleton="false">
<property name="transactionManager"><ref
local="transactionManager"/></property>
<property name="transactionAttributeSource">
<value>test.ITestTarget.foo=PROPAGATION_REQUIRED</value>
</property>
</bean>
<!--Applies MixinTransactionInterceptor to TestTarget -->
<bean id="TestInterceptor"
class="org.springframework.aop.framework.ProxyFactoryBean" singleton="true">
<property
name="interceptorNames"><value>MixinTransactionInterceptor,TestTarget</value
></property>
<property name="singleton"><value>false</value></property>
</bean>
My test is as simple as it can get ;-)
public class TestTarget implements ITestTarget
{
private int invocationCount = 0;
public void foo()
{
System.err.println
(
"<<TestTarget:foo>> "+toString()+" Invocation Count is
"+(++invocationCount)
);
}
}
ITestTarget testInterceptor1 =
(ITestTarget)context.getBean("TestInterceptor");
ITestTarget testInterceptor2 =
(ITestTarget)context.getBean("TestInterceptor");
testInterceptor1.foo();
testInterceptor2.foo();
Output is
[java] <<TestTarget:foo>> test.TestTarget@e183e9 Invocation Count is 1
[java] <<TestTarget:foo>> test.TestTarget@e183e9 Invocation Count is 2
On a related note, I think it would be _nice_ to be able to define the
transaction behaviour in the same manner
adopted by TransactionProxyFactoryBean.
Also, is it possible to apply my MixinTransactionInterceptor to multiple
target classes
<property name="transactionAttributeSource">
<value>
test.ITestTarget.foo=PROPAGATION_REQUIRED
test.IOtherObject.bar=PROPAGATION_REQUIRED
</value>
</property>
Thanks again,
Mark
-----Original Message-----
From: Rod Johnson [mailto:rod...@in...]
Sent: 20 November 2003 17:29
To: spr...@li...
Subject: Re: [Springframework-developer] Intercepting a Prototype Bean
Mark,
> Is it possible to apply an interceptor to a prototype bean?
Yes.
> The prototype is stateful - i.e one per user
Yes, Spring supports mixins, ie one interceptor per mixin instance.
> I have defined the following in applicationContext.xml
>
>
> <bean id="TestTarget" class="test.MyTarget" singleton="false">
> </bean>
>
>
> <bean id="TestInterceptor"
>
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBe
> an" singleton="false">
> <property name="transactionManager"><ref
> local="transactionManager"/></property>
>
> <property name="target"><ref local="TestTarget"/></property>
>
>
> <property name="transactionAttributes">
> <props>
> <prop key="foo">PROPAGATION_REQUIRED</prop>
> </props>
> </property>
> </bean>
>
>
>
> When I invoke the method foo I get a ClassCastException
Not sure why this is happening but you may need to create the interceptor
chain manually to use a prototype, as TransactionProxyFactoryBean propably
assumes it's dealing with a singleton. So you need to create a chain that
includes:
- the name of your prototype
- the interceptor names, which may be singletons or prototypes as you
require. Use a prototype for mixin support.
The following example comes from the test suite, with minor changes to make
it more obvious:
<bean id="prototypeTarget"
class="org.springframework.aop.interceptor.SideEffectBean"
singleton="false">
<property name="count"><value>10</value></property>
</bean>
<!-- This can be a singleton or a prototype (for mixin behaviour) -->
<bean id="debugInterceptor"
class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>
<bean id="prototype"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- will automatically create invoker interceptor for the prototype -->
<property
name="interceptorNames"><value>debugInterceptor,prototypeTarget</value></pro
perty>
<!-- Note this -->
<property name="singleton"><value>false</value></property>
</bean>
There is a bug in M2 to do with prototype AOP handling, which is fixed in
the forthcoming M3. However I don't think it should affect you in such a
simple usage.
Regards,
Rod
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
_______________________________________________
Springframework-developer mailing list
Spr...@li...
https://lists.sourceforge.net/lists/listinfo/springframework-developer
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/
_______________________________________________
Springframework-developer mailing list
Spr...@li...
https://lists.sourceforge.net/lists/listinfo/springframework-developer
|