Comment #7 on issue 172 by [email protected]: Enable injection after deserialization
http://code.google.com/p/google-guice/issues/detail?id=172

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);


--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to