Although the Bar instance reference is not retained by the Foo instance, the Bar instance is still technically a dependent instance of Foo and therefore the container needs to keep a reference to it in order to eventually destroy it properly.

This is a problem because dependent instances of a bean are currently supposed to be serialized together with the normal-scoped instance but the Bar instance is not serializable.

We may work around by making use of the following part of the spec:

Finally, the container is permitted to destroy any @Dependent scoped contextual instance at any time if the instance is no
longer referenced by the application (excluding weak, soft and phantom references)."

and destroy every non-serializable dependent bean instance of a normal-scoped bean when the creational context for the normal-scoped bean instance is serialized.

This is not 100% equivalent to "the instance is no longer referenced by the application" however covers all the sensible cases including:

Foo no longer holds a reference to Bar, therefore we can safely destroy Bar

Foo holds a reference to Bar - does not matter if we destroy Bar prematurely or not since serialization of Bar is going to fail anyway

Foo holds a reference to Bar in a transient field and is therefore either going to "forget" the reference no serialization (therefore we can destroy it) or is able to recreate the Bar instance itself on deserialization

Although the Bar instance reference is not retained by the Foo instance, the Bar instance is still technically a dependent instance of Foo and therefore the container needs to keep a reference to it in order to eventually destroy it properly.

We may make use of the following part of the spec:

{QUOTE}
Finally, the container is permitted to destroy any @Dependent scoped contextual instance at any time if the instance is no
longer referenced by the application (excluding weak, soft and phantom references)."
{QUOTE}

and destroy every non-serializable dependent bean instance of a normal-scoped bean when the creational context for the normal-scoped bean instance is serialized.

This should be safe for these cases:
* Foo no longer holds a reference to Bar, therefore we can destroy Bar
* Foo holds a reference to Bar - does not matter if we destroy Bar prematurely since serialization of Bar is going to fail anyway
* Foo holds a reference to Bar in a transient field and is therefore either going to "forget" the reference (therefore we can destroy it) or is able to recreate the Bar instance itself on deserialization

Although the Bar instance reference is not retained by the Foo instance, the Bar instance is still technically a dependent instance of Foo and therefore the container needs to keep a reference to it in order to eventually destroy it properly.

This is a problem because dependent instances of a bean are currently supposed to be serialized together with the normal-scoped instance but the Bar instance is not serializable.

We may work around by making use of the following part of the spec:

{QUOTE}
Finally, the container is permitted to destroy any @Dependent scoped contextual instance at any time if the instance is no
longer referenced by the application (excluding weak, soft and phantom references)."
{QUOTE}

and destroy every non-serializable dependent bean instance of a normal-scoped bean when the creational context for the normal-scoped bean instance is serialized.

This is not 100% equivalent to "the instance is no longer referenced by the application" however covers all the sensible cases including:
* Foo no longer holds a reference to Bar, therefore we can safely destroy Bar
* Foo holds a reference to Bar - does not matter if we destroy Bar prematurely or not since serialization of Bar is going to fail anyway
* Foo holds a reference to Bar in a transient field and is therefore either going to "forget" the reference no serialization (therefore we can destroy it) or is able to recreate the Bar instance itself on deserialization

Jozef Hartinger
added a comment - 16/Apr/12 5:48 AM Alternatively, we should not use creational context to store dependent instances that do not define @PreDestroy callbacks nor are they intercepted by a @PreDestroy interceptor.