You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Mauro Ciancio <ma...@gmail.com> on 2010/10/07 04:51:08 UTC

Bean injection

Hello everyone,
  I'm using spring in one wicket project and I'm lost about how the
proxy stuff works. I've realized that the fields marked with
@springbean are injected when the component injection listener runs.

  Also, I've looked in AnnotProxyFieldValueFactory.class to find out
how the bean proxy is created and how it can be serialized.

  Now, this is my issue. The page is created by wicket, the injection
is done and I have the bean ready for use. If the page is serialized,
the bean will be reconstructed using the bean factory (through the
proxy). But, somehow when I click a non bookmarkable link in my page,
the bean is never reconstructed and I have the previous reference to
the bean (I mean, the same object).

  Is this the correct behavior? Shouldn't this bean be reconstructed
when another request arrives?

  BTW, I've defined all the beans with prototype scope, so it's not a
spring issue.

  What am I missing?

Regards.
-- 
Mauro Ciancio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by James Carman <ja...@carmanconsulting.com>.
On Thu, Oct 7, 2010 at 9:50 AM, Mauro Ciancio <ma...@gmail.com> wrote:
> OK, I understand, that explains my issue. However, I have a bean that
> is stateful and the state cannot be sent by wicket componets (more
> precisely it's a bean that provides the local date and local time, and
> it's cached in a field so every time you invoke it, it returns the
> same date).
>
> So, I'm getting the same date when I navigate through a stateful page.
>
> Is there any another workaround or the only way out is not to keep the
> date cached?
>

So, every time you ask the bean, you want it to return the date/time
when it was created (or when it was first asked for the current
date/time)?  Is that an important date/time?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by mwilber <ma...@gmail.com>.
We have had to grab the beans ourselves for request scoped beans. In our case
they are trying to cache service call results that may change over time and
allows for data consistency through the request. If we use the @SpringBean
annotation for this we have seen that the bean is cached and not
"refetched". I have wanted to look more at this in the Proxy factory but
have not had a chance.
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Bean-injection-tp2966113p2967048.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by Mauro Ciancio <ma...@gmail.com>.
> For this particular use case (the current date), I would suggest you
> just calculate that on-the-fly, not at bean creation time.  It's not
> that expensive to calculate.

Yes, I appreciate your suggestion. :)
Thanks James.
Regards.
-- 
Mauro Ciancio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by James Carman <ja...@carmanconsulting.com>.
On Fri, Oct 8, 2010 at 10:42 AM, Mauro Ciancio <ma...@gmail.com> wrote:
>
> Wel, that explains the whole issue. If only if had known it before. :)
> Thanks to all for the replies.
> Regards.
> -

For this particular use case (the current date), I would suggest you
just calculate that on-the-fly, not at bean creation time.  It's not
that expensive to calculate.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by Mauro Ciancio <ma...@gmail.com>.
Hi,

>> Yes: after "deserialization" :). And yes: after deserialization of
>> yout page version this bean is relocated from Spring again. You can
>> chcek this: go to the previous page version by clicking "back" button
>> and press refresh button (if page is cached in a browser). Then you
>> can see that a new bean instance is created. When you stays on the
>> same page and clicking statefull links this page is not deserialized
>> (correct me if I am wrong).
>>
>
> Yes, the most recent version of the page is kept in the session
> (in-memory most likely).

Wel, that explains the whole issue. If only if had known it before. :)
Thanks to all for the replies.
Regards.
-- 
Mauro Ciancio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by James Carman <ja...@carmanconsulting.com>.
On Fri, Oct 8, 2010 at 4:30 AM, Daniel Stoch <da...@gmail.com> wrote:
>
> Yes: after "deserialization" :). And yes: after deserialization of
> yout page version this bean is relocated from Spring again. You can
> chcek this: go to the previous page version by clicking "back" button
> and press refresh button (if page is cached in a browser). Then you
> can see that a new bean instance is created. When you stays on the
> same page and clicking statefull links this page is not deserialized
> (correct me if I am wrong).
>

Yes, the most recent version of the page is kept in the session
(in-memory most likely).

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by Daniel Stoch <da...@gmail.com>.
On Thu, Oct 7, 2010 at 5:47 PM, Mauro Ciancio <ma...@gmail.com> wrote:
>
> It's not so important, but I can't figure out why I have the same bean
> instance, taking into account that the page is serialized at the end
> of the request and the inyected bean is actually a proxy of the bean,
> so no real bean is in the serialization.
>
> I've double checked it, and the _same_ bean instance is holded in my
> page en each request to the same page.
>
> Shouldn't the bean get relocated after serialization?
>

Yes: after "deserialization" :). And yes: after deserialization of
yout page version this bean is relocated from Spring again. You can
chcek this: go to the previous page version by clicking "back" button
and press refresh button (if page is cached in a browser). Then you
can see that a new bean instance is created. When you stays on the
same page and clicking statefull links this page is not deserialized
(correct me if I am wrong).

--
Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by Mauro Ciancio <ma...@gmail.com>.
Hi, thanks for the quick replies.

> I think as a WorkAround is to get the beans yourself from the Spring context
> insteads of relying on the @SpringBean annotation

Yes, that would work.

> The code specifically checks to see if the bean is a singleton before
> it caches it.  Non-singleton beans should not be cached:
>
> http://svn.apache.org/repos/asf/wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java
>
> Search for "isSingletonBean"

Yes, I've looked at that code and that's why I'm confused. I meant,
the bean is inyected properly when the page is constructed (via page
constructor, fresh bean). But when I stay in the same page instance
(using stateful links for example), in a later request I have the same
bean instance.

> So, every time you ask the bean, you want it to return the date/time
> when it was created (or when it was first asked for the current
> date/time)?  Is that an important date/time?

It's not so important, but I can't figure out why I have the same bean
instance, taking into account that the page is serialized at the end
of the request and the inyected bean is actually a proxy of the bean,
so no real bean is in the serialization.

I've double checked it, and the _same_ bean instance is holded in my
page en each request to the same page.

Shouldn't the bean get relocated after serialization?

Thanks again :)
Regards.
-- 
Mauro Ciancio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by jcgarciam <jc...@gmail.com>.
I think as a WorkAround is to get the beans yourself from the Spring context
insteads of relying on the @SpringBean annotation

On Thu, Oct 7, 2010 at 10:51 AM, Mauro Ciancio [via Apache Wicket] <
ml-node+2966817-1959417264-65838@n4.nabble.com<ml...@n4.nabble.com>
> wrote:

> Hi,
>
> > Why are you using prototype scope anyway?
>
> In order to get a new fresh instance every time is requested.
>
> > AFAIR the once the bean is looked up from spring it is being cached by
> > AnnotProxyFieldValueFactory itself.
> >
> > So using prototype scope will not work anyway: either way you always get
> a
> > fresh bean (so you cannot keep state) or you will always get the same
> bean
> > (even worse if the bean is stateful).
> >
> > You probably should convert your code to using singleton beans and pass
> > appropriate state from wicket component itself.
>
> OK, I understand, that explains my issue. However, I have a bean that
> is stateful and the state cannot be sent by wicket componets (more
> precisely it's a bean that provides the local date and local time, and
> it's cached in a field so every time you invoke it, it returns the
> same date).
>
> So, I'm getting the same date when I navigate through a stateful page.
>
> Is there any another workaround or the only way out is not to keep the
> date cached?
>
> Thanks for the replies.
> Regards.
> --
> Mauro Ciancio
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=2966817&i=0>
> For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=2966817&i=1>
>
>
>
> ------------------------------
>  View message @
> http://apache-wicket.1842946.n4.nabble.com/Bean-injection-tp2966113p2966817.html
> To start a new topic under Apache Wicket, email
> ml-node+1842946-398011874-65838@n4.nabble.com<ml...@n4.nabble.com>
> To unsubscribe from Apache Wicket, click here<http://apache-wicket.1842946.n4.nabble.com/template/TplServlet.jtp?tpl=unsubscribe_by_code&node=1842946&code=amNnYXJjaWFtQGdtYWlsLmNvbXwxODQyOTQ2fDEyNTYxMzc3ODY=>.
>
>
>


-- 
Sincerely,
JC (http://www.linkedin.com/in/jcgarciam)
Work smarter, not harder!.

-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Bean-injection-tp2966113p2966851.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by Mauro Ciancio <ma...@gmail.com>.
Hi,

> Why are you using prototype scope anyway?

In order to get a new fresh instance every time is requested.

> AFAIR the once the bean is looked up from spring it is being cached by
> AnnotProxyFieldValueFactory itself.
>
> So using prototype scope will not work anyway: either way you always get a
> fresh bean (so you cannot keep state) or you will always get the same bean
> (even worse if the bean is stateful).
>
> You probably should convert your code to using singleton beans and pass
> appropriate state from wicket component itself.

OK, I understand, that explains my issue. However, I have a bean that
is stateful and the state cannot be sent by wicket componets (more
precisely it's a bean that provides the local date and local time, and
it's cached in a field so every time you invoke it, it returns the
same date).

So, I'm getting the same date when I navigate through a stateful page.

Is there any another workaround or the only way out is not to keep the
date cached?

Thanks for the replies.
Regards.
-- 
Mauro Ciancio

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by James Carman <ja...@carmanconsulting.com>.
On Wed, Oct 6, 2010 at 10:51 PM, Mauro Ciancio <ma...@gmail.com> wrote:
>  BTW, I've defined all the beans with prototype scope, so it's not a
> spring issue.
>

Why are you using prototype scope anyway?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by James Carman <ja...@carmanconsulting.com>.
On Thu, Oct 7, 2010 at 9:13 AM, Leszek Gawron <lg...@apache.org> wrote:
> AFAIR the once the bean is looked up from spring it is being cached by
> AnnotProxyFieldValueFactory itself.
>
> So using prototype scope will not work anyway: either way you always get a
> fresh bean (so you cannot keep state) or you will always get the same bean
> (even worse if the bean is stateful).
>

The code specifically checks to see if the bean is a singleton before
it caches it.  Non-singleton beans should not be cached:

http://svn.apache.org/repos/asf/wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java

Search for "isSingletonBean"

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Bean injection

Posted by Leszek Gawron <lg...@apache.org>.
On 2010-10-07 04:51, Mauro Ciancio wrote:
> Hello everyone,
>    I'm using spring in one wicket project and I'm lost about how the
> proxy stuff works. I've realized that the fields marked with
> @springbean are injected when the component injection listener runs.
>
>    Also, I've looked in AnnotProxyFieldValueFactory.class to find out
> how the bean proxy is created and how it can be serialized.
>
>    Now, this is my issue. The page is created by wicket, the injection
> is done and I have the bean ready for use. If the page is serialized,
> the bean will be reconstructed using the bean factory (through the
> proxy). But, somehow when I click a non bookmarkable link in my page,
> the bean is never reconstructed and I have the previous reference to
> the bean (I mean, the same object).
>
>    Is this the correct behavior? Shouldn't this bean be reconstructed
> when another request arrives?
>
>    BTW, I've defined all the beans with prototype scope, so it's not a
> spring issue.

AFAIR the once the bean is looked up from spring it is being cached by 
AnnotProxyFieldValueFactory itself.

So using prototype scope will not work anyway: either way you always get 
a fresh bean (so you cannot keep state) or you will always get the same 
bean (even worse if the bean is stateful).

You probably should convert your code to using singleton beans and pass 
appropriate state from wicket component itself.

	lg

-- 
Leszek Gawron                              http://lgawron.blogspot.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org