You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Andreas Petersson <an...@petersson.at> on 2009/02/06 00:46:50 UTC
IoC: how to best handle non-serializable fields in wicket
hi!
so, im using wicket together with guice for DI.
i really wondered that the correct approach is here.
at first i thought it way easy to do this, but it turns out it is more
complicated..- at least more complicated to understand.
my typical page looks like this:
public class StartPage extends WebPage {
private transient EmkDao dao;
@Inject
public void setDao(EmkDao dao) {
this.dao = dao;
}
public EmkDao getDao() {
return dao;
}
public Start() {
add( Components that use the dao in various ways (models, onclick etc);
}
}
this seems to work well, in simple cases. BUT! as soon as i press
browser-back and reload most likely the dao member has become null. -> NPE
the expected behavior would be: transient fields are re-injected
whenever the page is deserialized. obviously this is not the case.
im surprised, that my StartPage is often a different instance (as seen
in the debugger) , when using browser-back-reload, but the setter
annotated with @Inject is not called twice.
since WebPage is Serializable and most injected objects are not (they
often contain references to non-serializable classes) they have to be
declared transient.
I use the following guice config:
<filter>
<filter-name>WicketApplication</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.guice.GuiceWebApplicationFactory</param-value>
</init-param>
<init-param>
<param-name>injectorContextAttribute</param-name>
<param-value>com.google.inject.Injector</param-value>
</init-param>
</filter>
with
bind(WebApplication.class).to(MyWicketApplication.class) in my Module.
i also tried the configuration described at
http://www.wideplay.com/wicketwithwarp-servlet with a simple
WicketFilter (without the GuiceWebApplicationFactory) and
Integrathe.with(this) in the WicketApplication, with the same result.
As I'm writing this mail, it turns out, it appears to work if i just
omit the "transient" keyword.
i wonder: what will that do to wicket pages? will i prevent them to
serialize any more? why doesn't it throw a NotSerializableException ?
then i have to use
@SuppressWarnings({"NonSerializableFieldInSerializableClass"}) for
IntelliJ to omit the warning.
somehow i am missing a coherent documentation on wicket+IoC on this matter.
best regards,
Andreas
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Andreas Petersson <an...@petersson.at>.
Timo Rantalaiho schrieb:
> On Fri, 06 Feb 2009, Andreas Petersson wrote:
>
>> i wonder: myDao is most likely not serializable.
>> WebPage is.
>> WebPage should get serialized.
>> so why the heck does this not throw an error? what kind of magic is going
>> on here?
>>
>
> Look at what GuiceComponentInjector does (or something it
> calls).
>
> The access to the dao is proxied by a serialisable proxy.
> The proxy can be serialised and deserialised without
> problems, and it will access the real dao.
>
> Best wishes,
> Timo
>
>
ok, now the puzzle pieces come together. as described at
http://cwiki.apache.org/WICKET/guice-integration-pitfall.html there are
proxies/CGlib subclasses created. - now i understand why these are
necessary, and why it does not matter that they are not serializable.
it all makes sense now :) thanks a lot.
tough a comment by Igor Vaynberg
<https://issues.apache.org/jira/secure/ViewProfile.jspa?name=ivaynberg>
at https://issues.apache.org/jira/browse/WICKET-1130 made me wonder:
guice already uses annotations, so - at least for it - it would be
possible to skip the proxying part.
otoh, EasyMock Class Extension also creates instances of classes, even
without protected no-args constructor, so i guess there must be a way to
do it.
-------
/Igor Vaynberg
<https://issues.apache.org/jira/secure/ViewProfile.jspa?name=ivaynberg>
- 24/Mar/08 04:56 PM/
/moving this to 1.5.
basically when injecting instances of concrete classes we are limited by
cglib which requires a default visible constructor and no final methods
so that it can create a subclass for our proxy.
once we switch to jdk5 we can require usage of annotations for injection
and then we can remove the proxies and use hooks in page serialization
to inject objects and hooks in deserialzation to make sure injected
fields are skipped. /
[ Show » <https://issues.apache.org/jira/browse/WICKET-1130> ]
Igor Vaynberg
<https://issues.apache.org/jira/secure/ViewProfile.jspa?name=ivaynberg>
- 24/Mar/08 04:56 PM moving this to 1.5. basically when injecting
instances of concrete classes we are limited by cglib which requires a
default visible constructor and no final methods so that it can create a
subclass for our proxy. once we switch to jdk5 we can require usage of
annotations for injection and then we can remove the proxies and use
hooks in page serialization to inject objects and hooks in
deserialzation to make sure injected fields are skipped.
/---------
/
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Fri, 06 Feb 2009, Andreas Petersson wrote:
> i wonder: myDao is most likely not serializable.
> WebPage is.
> WebPage should get serialized.
> so why the heck does this not throw an error? what kind of magic is going
> on here?
Look at what GuiceComponentInjector does (or something it
calls).
The access to the dao is proxied by a serialisable proxy.
The proxy can be serialised and deserialised without
problems, and it will access the real dao.
Best wishes,
Timo
--
Timo Rantalaiho
Reaktor Innovations Oy <URL: http://www.ri.fi/ >
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by James Carman <jc...@carmanconsulting.com>.
If the interface extends Serializable and you implement it, you
implement Serializable. That's not a good idea for service/dao
classes.
On Thu, Feb 5, 2009 at 7:12 PM, Adriano dos Santos Fernandes
<ad...@gmail.com> wrote:
> Andreas Petersson wrote:
>>
>> hi timo, thanks for the links.
>>
>> well, i've modeled my application after reading the mentioned example.
>>
>> from Wicket, Guice and Ibatis example:
>>
>> public class MyPage extends WebPage {
>> @Inject
>> protected MyDao myDao;
>>
>>
>> -------
>> i wonder: myDao is most likely not serializable.
>> WebPage is. WebPage should get serialized. so why the heck does this not
>> throw an error? what kind of magic is going on here?
>
> My understand (of a guice newbie) is that your DAO class is not required
> (and will not be, using guice) serializable. But you should have a
> serializable interface. Wicket-ioc/guice will create a serializable proxy
> with a transient instance of the implementation. When it's null, a instance
> will be reinjected reinjected.
>
>
> Adriano
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Adriano dos Santos Fernandes <ad...@gmail.com>.
Andreas Petersson wrote:
> hi timo, thanks for the links.
>
> well, i've modeled my application after reading the mentioned example.
>
> from Wicket, Guice and Ibatis example:
>
> public class MyPage extends WebPage {
> @Inject
> protected MyDao myDao;
>
>
> -------
> i wonder: myDao is most likely not serializable.
> WebPage is. WebPage should get serialized. so why the heck does this
> not throw an error? what kind of magic is going on here?
My understand (of a guice newbie) is that your DAO class is not required
(and will not be, using guice) serializable. But you should have a
serializable interface. Wicket-ioc/guice will create a serializable
proxy with a transient instance of the implementation. When it's null, a
instance will be reinjected reinjected.
Adriano
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Andreas Petersson <an...@petersson.at>.
Timo Rantalaiho schrieb:
> On Fri, 06 Feb 2009, Andreas Petersson wrote:
>
>> so, im using wicket together with guice for DI.
>> i really wondered that the correct approach is here.
>>
>
> http://wicketstuff.org/wicket13/guice/
>
> http://cwiki.apache.org/WICKET/wicket-guice-and-ibatis-example.html
>
> Best wishes,
> Timo
>
hi timo, thanks for the links.
well, i've modeled my application after reading the mentioned example.
from Wicket, Guice and Ibatis example:
public class MyPage extends WebPage {
@Inject
protected MyDao myDao;
-------
i wonder: myDao is most likely not serializable.
WebPage is.
WebPage should get serialized.
so why the heck does this not throw an error? what kind of magic is going on here?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Fri, 06 Feb 2009, Andreas Petersson wrote:
> so, im using wicket together with guice for DI.
> i really wondered that the correct approach is here.
http://wicketstuff.org/wicket13/guice/
http://cwiki.apache.org/WICKET/wicket-guice-and-ibatis-example.html
Best wishes,
Timo
--
Timo Rantalaiho
Reaktor Innovations Oy <URL: http://www.ri.fi/ >
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by francisco treacy <fr...@gmail.com>.
proxy i meant as in the proxy pattern - it delegates
francisco
On Fri, Feb 6, 2009 at 8:41 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> salve doesnt add a proxy, it removes the field and rewrites any field
> read with a lookup.
>
> -igor
>
> On Fri, Feb 6, 2009 at 11:32 AM, francisco treacy
> <fr...@gmail.com> wrote:
>> another option is to use salve (http://code.google.com/p/salve/).
>> instead of calling guice's injector as a service locator you use
>> @Dependency, that will lookup and inject your dependency (behind the
>> scenes it adds the class a proxy through bytecode instrumentation).
>>
>> public class myLDM extends LoadableDetachableModel {
>>
>> @Dependency myDAO;
>>
>> public Object load() {
>> return myDAO.get(...);
>> }
>>
>> francisco
>>
>> On Fri, Feb 6, 2009 at 3:06 PM, Martin Grigorov <mc...@e-card.bg> wrote:
>>> I have seen such code somewhere:
>>>
>>>
>>> public class myLDM extends LoadableDetachableModel {
>>>
>>> transient @SpringBean myDAO;
>>>
>>> public Object load() {
>>> InjectorHolder.getInjector().inject(this);
>>> return myDAO.get(...);
>>> }
>>>
>>> }
>>>
>>> El vie, 06-02-2009 a las 13:21 +0100, Martijn Dashorst escribió:
>>>> How would you inject the dao then?
>>>>
>>>> Martijn
>>>>
>>>>
>>>> On Fri, Feb 6, 2009 at 12:00 PM, Sergey Didenko
>>>> <se...@gmail.com> wrote:
>>>> > Hi Andreas,
>>>> >
>>>> > Well, may be my question is silly, but:
>>>> >
>>>> > What about making "dao" a local variable in your component handlers?
>>>> > Then you can get rid of that serializing-deserializing of dao as a
>>>> > Component member.
>>>> >
>>>> > Does this approach have too much overhead? Does it make some things impossible?
>>>> >
>>>> > Cheers, Sergey.
>>>> >
>>>> > ---------------------------------------------------------------------
>>>> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>> > For additional commands, e-mail: users-help@wicket.apache.org
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Igor Vaynberg <ig...@gmail.com>.
salve doesnt add a proxy, it removes the field and rewrites any field
read with a lookup.
-igor
On Fri, Feb 6, 2009 at 11:32 AM, francisco treacy
<fr...@gmail.com> wrote:
> another option is to use salve (http://code.google.com/p/salve/).
> instead of calling guice's injector as a service locator you use
> @Dependency, that will lookup and inject your dependency (behind the
> scenes it adds the class a proxy through bytecode instrumentation).
>
> public class myLDM extends LoadableDetachableModel {
>
> @Dependency myDAO;
>
> public Object load() {
> return myDAO.get(...);
> }
>
> francisco
>
> On Fri, Feb 6, 2009 at 3:06 PM, Martin Grigorov <mc...@e-card.bg> wrote:
>> I have seen such code somewhere:
>>
>>
>> public class myLDM extends LoadableDetachableModel {
>>
>> transient @SpringBean myDAO;
>>
>> public Object load() {
>> InjectorHolder.getInjector().inject(this);
>> return myDAO.get(...);
>> }
>>
>> }
>>
>> El vie, 06-02-2009 a las 13:21 +0100, Martijn Dashorst escribió:
>>> How would you inject the dao then?
>>>
>>> Martijn
>>>
>>>
>>> On Fri, Feb 6, 2009 at 12:00 PM, Sergey Didenko
>>> <se...@gmail.com> wrote:
>>> > Hi Andreas,
>>> >
>>> > Well, may be my question is silly, but:
>>> >
>>> > What about making "dao" a local variable in your component handlers?
>>> > Then you can get rid of that serializing-deserializing of dao as a
>>> > Component member.
>>> >
>>> > Does this approach have too much overhead? Does it make some things impossible?
>>> >
>>> > Cheers, Sergey.
>>> >
>>> > ---------------------------------------------------------------------
>>> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> > For additional commands, e-mail: users-help@wicket.apache.org
>>> >
>>> >
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by francisco treacy <fr...@gmail.com>.
another option is to use salve (http://code.google.com/p/salve/).
instead of calling guice's injector as a service locator you use
@Dependency, that will lookup and inject your dependency (behind the
scenes it adds the class a proxy through bytecode instrumentation).
public class myLDM extends LoadableDetachableModel {
@Dependency myDAO;
public Object load() {
return myDAO.get(...);
}
francisco
On Fri, Feb 6, 2009 at 3:06 PM, Martin Grigorov <mc...@e-card.bg> wrote:
> I have seen such code somewhere:
>
>
> public class myLDM extends LoadableDetachableModel {
>
> transient @SpringBean myDAO;
>
> public Object load() {
> InjectorHolder.getInjector().inject(this);
> return myDAO.get(...);
> }
>
> }
>
> El vie, 06-02-2009 a las 13:21 +0100, Martijn Dashorst escribió:
>> How would you inject the dao then?
>>
>> Martijn
>>
>>
>> On Fri, Feb 6, 2009 at 12:00 PM, Sergey Didenko
>> <se...@gmail.com> wrote:
>> > Hi Andreas,
>> >
>> > Well, may be my question is silly, but:
>> >
>> > What about making "dao" a local variable in your component handlers?
>> > Then you can get rid of that serializing-deserializing of dao as a
>> > Component member.
>> >
>> > Does this approach have too much overhead? Does it make some things impossible?
>> >
>> > Cheers, Sergey.
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> > For additional commands, e-mail: users-help@wicket.apache.org
>> >
>> >
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Sergey Didenko <se...@gmail.com>.
I see, it's against Guice philisophy:
http://docs.google.com/Doc?id=dd2fhx4z_5df5hw8
> The idea of bootstrapping is fundamental to dependency injection. Always explicitly asking the Injector for dependencies would be using Guice as a service locator, not a dependency injection framework.
>
> Your code should deal directly with the Injector as little as possible
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Sergey Didenko <se...@gmail.com>.
Yes, I mean something like this, with injector initialized in WebApplication.
On Fri, Feb 6, 2009 at 4:06 PM, Martin Grigorov <mc...@e-card.bg> wrote:
> I have seen such code somewhere:
>
>
> public class myLDM extends LoadableDetachableModel {
>
> transient @SpringBean myDAO;
>
> public Object load() {
> InjectorHolder.getInjector().inject(this);
> return myDAO.get(...);
> }
>
> }
>
> El vie, 06-02-2009 a las 13:21 +0100, Martijn Dashorst escribió:
>> How would you inject the dao then?
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Martin Grigorov <mc...@e-card.bg>.
I have seen such code somewhere:
public class myLDM extends LoadableDetachableModel {
transient @SpringBean myDAO;
public Object load() {
InjectorHolder.getInjector().inject(this);
return myDAO.get(...);
}
}
El vie, 06-02-2009 a las 13:21 +0100, Martijn Dashorst escribió:
> How would you inject the dao then?
>
> Martijn
>
>
> On Fri, Feb 6, 2009 at 12:00 PM, Sergey Didenko
> <se...@gmail.com> wrote:
> > Hi Andreas,
> >
> > Well, may be my question is silly, but:
> >
> > What about making "dao" a local variable in your component handlers?
> > Then you can get rid of that serializing-deserializing of dao as a
> > Component member.
> >
> > Does this approach have too much overhead? Does it make some things impossible?
> >
> > Cheers, Sergey.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Martijn Dashorst <ma...@gmail.com>.
How would you inject the dao then?
Martijn
On Fri, Feb 6, 2009 at 12:00 PM, Sergey Didenko
<se...@gmail.com> wrote:
> Hi Andreas,
>
> Well, may be my question is silly, but:
>
> What about making "dao" a local variable in your component handlers?
> Then you can get rid of that serializing-deserializing of dao as a
> Component member.
>
> Does this approach have too much overhead? Does it make some things impossible?
>
> Cheers, Sergey.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
--
Become a Wicket expert, learn from the best: http://wicketinaction.com
Apache Wicket 1.3.5 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: IoC: how to best handle non-serializable fields in wicket
Posted by Sergey Didenko <se...@gmail.com>.
Hi Andreas,
Well, may be my question is silly, but:
What about making "dao" a local variable in your component handlers?
Then you can get rid of that serializing-deserializing of dao as a
Component member.
Does this approach have too much overhead? Does it make some things impossible?
Cheers, Sergey.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org