bineanzhou / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Enable injection after deserialization #172

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
We are using Beehive page flow controllers which are being injected with
services. Since we rely on session replication the page flow controllers
will eventually be serialized. Two problems arise:

1) Since the injected services can not be serializable themselves, they are
marked as transient fields, that naturally tend to be null after
deserialization. How do I re-inject dependencies after deserialization?
This is the main issue of this ticket.

2) Any method interceptors bound to objects being serialized will cause the
serialization to fail with a NotSerializableException:
InterceptorStackCallback. I probably will file a seperate ticket for this
issue.

Original issue reported on code.google.com by phjar...@gmail.com on 20 Dec 2007 at 10:45

GoogleCodeExporter commented 9 years ago
Regarding 1)

Is Injector.injectMembers(Object) sufficient to you?

Original comment by sven.lin...@gmail.com on 15 Apr 2008 at 4:12

GoogleCodeExporter commented 9 years ago
Within the context of deserialization (java.io.Serializable.readObject()) there 
is no
way to get a reference to the injector instance, is there?

Original comment by phjar...@gmail.com on 15 Apr 2008 at 4:16

GoogleCodeExporter commented 9 years ago
Regarding 2)

Letting your InvocationHandler, MethodInterceptor or whatever you use implement
java.io.Externalizable and delegate the serialization to the origin Object what 
solve
this problem, as far as I see.

Original comment by sven.lin...@gmail.com on 15 Apr 2008 at 4:21

GoogleCodeExporter commented 9 years ago
Unfortunately I don't have control over the parent object (which would be the 
HTTP
session being serialized). =(

Original comment by phjar...@gmail.com on 2 Jun 2008 at 9:05

GoogleCodeExporter commented 9 years ago
You need to hook into your session serialization mechanism, and make your 
Injector available to the 
deserializing code. If anyone in the user's community has an implementation, 
please post it here for review.

Original comment by limpbizkit on 3 Jun 2008 at 9:42

GoogleCodeExporter commented 9 years ago
Been open for a couple years and no one's posted anything more.  Guice already 
provides all the necessary hooks for making this work, typically using 
Injector.injectMembers.

Original comment by sberlin on 19 Feb 2011 at 8:32

GoogleCodeExporter commented 9 years ago
Just in case it's useful to other people (this issue is still one of the top 
hits for a google search of "deserialize guice injected object"), here is one 
possible solution..

In the class being deserialized, mark relevant injected objects as transient. 
Then:

@Inject
private static Provider<MyClass> provider;

/**
 * When deserializing this object, use Guice to create an instance rather than
 * using the default behaviour of invoking Class.newInstance() which in turn
 * invokes the no-args constructor. This ensures that any Guice configuration
 * occurs, including both constructor and member injection.
 */
private Object readResolve() throws ObjectStreamException {
  return provider.get();
}

and in a guice module, ensure the provider field is initialised via

  requestStaticInjection(MyClass.class);

Original comment by simon.ki...@airnz.co.nz on 18 Jul 2011 at 9:27

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I've came to following solution; I'm new with Guice so maybe I've missed 
something important.

I think that use of injector and method injection is justified in this case, as 
this code is should be part of lower level infrastructure.

Original comment by fima.rot...@gmail.com on 24 Apr 2014 at 1:21

Attachments: