RE: [picocontainer-dev] Constructor Injection and Serialization Patterns

Jörg Schaible <[email protected]>
Newsgroups gmane.comp.java.picocontainer.devel
Message-ID <[email protected]>
Hi Michael,


  _____  

	From: Michael Rimov [mailto:[email protected]] 
	Sent: Tuesday, February 26, 2008 8:15 PM
	To: dev-qxt/[email protected]
	Subject: RE: [picocontainer-dev] Constructor Injection and Serialization Patterns
	
	

	Sorry for not getting it, but I don't think I understand how Objenesis would help with this problem?

	 

	What I want is to be able to:

	 

	ObjectInputStream ois = new ObjectInputStream( /* some input stream */ );

	Sample s = (Sample)ois.readObject();

	 

	/* And then a miracle occurs! */

	 

	assert s.getClassLoader() != null;

	 

	And somehow have Sample useful, even if the classloader specified in the constructor would have to be transient. (Or just not saved via custom serialization/externalization).  

	 

	So far, the only things I can think of are atatic containers and have class Sample read its own dependencies during readObject()... which of course, breaks everything in the Pico book in only one function. :-) 

Not really, since serialization is also not covered by DI. :)

Since it is the DI container that is normally repsonsible for creating a new object, you're in conflict with the serialization anyway.

 

Therefore I'd make the ClassLoader transient.

	Does this clarify my problem?  I suspect this isn't really anything Pico can solve per-se, rather a standard pattern for dealing with this problem would be a good thing to document. 

The readResolve method will not realy solve your problem, since you'll have to get the correct ClassLoader form somewhere also. For the Pico way I can only think of a SerializingTransiendFieldInjector i.e. a factory that creates an object from an object stream and does a field injection for all transient fields (handle them as if they are the dependencies). However, where does now the object stream come from ?

 

- Jörg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.